dir.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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. * @parent_sd: parent sysfs_dirent
  342. *
  343. * This function is called when the caller is about to add or
  344. * remove sysfs_dirent under @parent_sd. This function acquires
  345. * sysfs_mutex. @acxt is used to keep and pass context to
  346. * other addrm functions.
  347. *
  348. * LOCKING:
  349. * Kernel thread context (may sleep). sysfs_mutex is locked on
  350. * return.
  351. */
  352. void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt,
  353. struct sysfs_dirent *parent_sd)
  354. {
  355. memset(acxt, 0, sizeof(*acxt));
  356. acxt->parent_sd = parent_sd;
  357. mutex_lock(&sysfs_mutex);
  358. }
  359. /**
  360. * __sysfs_add_one - add sysfs_dirent to parent without warning
  361. * @acxt: addrm context to use
  362. * @sd: sysfs_dirent to be added
  363. *
  364. * Get @acxt->parent_sd and set sd->s_parent to it and increment
  365. * nlink of parent inode if @sd is a directory and link into the
  366. * children list of the parent.
  367. *
  368. * This function should be called between calls to
  369. * sysfs_addrm_start() and sysfs_addrm_finish() and should be
  370. * passed the same @acxt as passed to sysfs_addrm_start().
  371. *
  372. * LOCKING:
  373. * Determined by sysfs_addrm_start().
  374. *
  375. * RETURNS:
  376. * 0 on success, -EEXIST if entry with the given name already
  377. * exists.
  378. */
  379. int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
  380. {
  381. struct sysfs_inode_attrs *ps_iattr;
  382. int ret;
  383. sd->s_hash = sysfs_name_hash(sd->s_name, sd->s_ns);
  384. sd->s_parent = sysfs_get(acxt->parent_sd);
  385. ret = sysfs_link_sibling(sd);
  386. if (ret)
  387. return ret;
  388. /* Update timestamps on the parent */
  389. ps_iattr = acxt->parent_sd->s_iattr;
  390. if (ps_iattr) {
  391. struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
  392. ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
  393. }
  394. /* Mark the entry added into directory tree */
  395. sd->s_flags &= ~SYSFS_FLAG_REMOVED;
  396. return 0;
  397. }
  398. /**
  399. * sysfs_pathname - return full path to sysfs dirent
  400. * @sd: sysfs_dirent whose path we want
  401. * @path: caller allocated buffer of size PATH_MAX
  402. *
  403. * Gives the name "/" to the sysfs_root entry; any path returned
  404. * is relative to wherever sysfs is mounted.
  405. */
  406. static char *sysfs_pathname(struct sysfs_dirent *sd, char *path)
  407. {
  408. if (sd->s_parent) {
  409. sysfs_pathname(sd->s_parent, path);
  410. strlcat(path, "/", PATH_MAX);
  411. }
  412. strlcat(path, sd->s_name, PATH_MAX);
  413. return path;
  414. }
  415. /**
  416. * sysfs_add_one - add sysfs_dirent to parent
  417. * @acxt: addrm context to use
  418. * @sd: sysfs_dirent to be added
  419. *
  420. * Get @acxt->parent_sd and set sd->s_parent to it and increment
  421. * nlink of parent inode if @sd is a directory and link into the
  422. * children 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. {
  437. int ret;
  438. ret = __sysfs_add_one(acxt, sd);
  439. if (ret == -EEXIST) {
  440. char *path = kzalloc(PATH_MAX, GFP_KERNEL);
  441. WARN(1, KERN_WARNING
  442. "sysfs: cannot create duplicate filename '%s'\n",
  443. (path == NULL) ? sd->s_name
  444. : (sysfs_pathname(acxt->parent_sd, path),
  445. strlcat(path, "/", PATH_MAX),
  446. strlcat(path, sd->s_name, PATH_MAX),
  447. path));
  448. kfree(path);
  449. }
  450. return ret;
  451. }
  452. /**
  453. * sysfs_remove_one - remove sysfs_dirent from parent
  454. * @acxt: addrm context to use
  455. * @sd: sysfs_dirent to be removed
  456. *
  457. * Mark @sd removed and drop nlink of parent inode if @sd is a
  458. * directory. @sd is unlinked from the children list.
  459. *
  460. * This function should be called between calls to
  461. * sysfs_addrm_start() and sysfs_addrm_finish() and should be
  462. * passed the same @acxt as passed to sysfs_addrm_start().
  463. *
  464. * LOCKING:
  465. * Determined by sysfs_addrm_start().
  466. */
  467. void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
  468. {
  469. struct sysfs_inode_attrs *ps_iattr;
  470. BUG_ON(sd->s_flags & SYSFS_FLAG_REMOVED);
  471. sysfs_unlink_sibling(sd);
  472. /* Update timestamps on the parent */
  473. ps_iattr = acxt->parent_sd->s_iattr;
  474. if (ps_iattr) {
  475. struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
  476. ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
  477. }
  478. sd->s_flags |= SYSFS_FLAG_REMOVED;
  479. sd->u.removed_list = acxt->removed;
  480. acxt->removed = sd;
  481. }
  482. /**
  483. * sysfs_addrm_finish - finish up sysfs_dirent add/remove
  484. * @acxt: addrm context to finish up
  485. *
  486. * Finish up sysfs_dirent add/remove. Resources acquired by
  487. * sysfs_addrm_start() are released and removed sysfs_dirents are
  488. * cleaned up.
  489. *
  490. * LOCKING:
  491. * sysfs_mutex is released.
  492. */
  493. void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
  494. {
  495. /* release resources acquired by sysfs_addrm_start() */
  496. mutex_unlock(&sysfs_mutex);
  497. /* kill removed sysfs_dirents */
  498. while (acxt->removed) {
  499. struct sysfs_dirent *sd = acxt->removed;
  500. acxt->removed = sd->u.removed_list;
  501. sysfs_deactivate(sd);
  502. unmap_bin_file(sd);
  503. sysfs_put(sd);
  504. }
  505. }
  506. /**
  507. * sysfs_find_dirent - find sysfs_dirent with the given name
  508. * @parent_sd: sysfs_dirent to search under
  509. * @name: name to look for
  510. * @ns: the namespace tag to use
  511. *
  512. * Look for sysfs_dirent with name @name under @parent_sd.
  513. *
  514. * LOCKING:
  515. * mutex_lock(sysfs_mutex)
  516. *
  517. * RETURNS:
  518. * Pointer to sysfs_dirent if found, NULL if not.
  519. */
  520. struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
  521. const unsigned char *name,
  522. const void *ns)
  523. {
  524. struct rb_node *node = parent_sd->s_dir.children.rb_node;
  525. unsigned int hash;
  526. hash = sysfs_name_hash(name, ns);
  527. while (node) {
  528. struct sysfs_dirent *sd;
  529. int result;
  530. sd = to_sysfs_dirent(node);
  531. result = sysfs_name_compare(hash, name, ns, sd);
  532. if (result < 0)
  533. node = node->rb_left;
  534. else if (result > 0)
  535. node = node->rb_right;
  536. else
  537. return sd;
  538. }
  539. return NULL;
  540. }
  541. /**
  542. * sysfs_get_dirent_ns - find and get sysfs_dirent with the given name
  543. * @parent_sd: sysfs_dirent to search under
  544. * @name: name to look for
  545. * @ns: the namespace tag to use
  546. *
  547. * Look for sysfs_dirent with name @name under @parent_sd and get
  548. * it if found.
  549. *
  550. * LOCKING:
  551. * Kernel thread context (may sleep). Grabs sysfs_mutex.
  552. *
  553. * RETURNS:
  554. * Pointer to sysfs_dirent if found, NULL if not.
  555. */
  556. struct sysfs_dirent *sysfs_get_dirent_ns(struct sysfs_dirent *parent_sd,
  557. const unsigned char *name,
  558. const void *ns)
  559. {
  560. struct sysfs_dirent *sd;
  561. mutex_lock(&sysfs_mutex);
  562. sd = sysfs_find_dirent(parent_sd, name, ns);
  563. sysfs_get(sd);
  564. mutex_unlock(&sysfs_mutex);
  565. return sd;
  566. }
  567. EXPORT_SYMBOL_GPL(sysfs_get_dirent_ns);
  568. static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
  569. const char *name, const void *ns,
  570. struct sysfs_dirent **p_sd)
  571. {
  572. umode_t mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
  573. struct sysfs_addrm_cxt acxt;
  574. struct sysfs_dirent *sd;
  575. int rc;
  576. /* allocate */
  577. sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
  578. if (!sd)
  579. return -ENOMEM;
  580. sd->s_ns = ns;
  581. sd->s_dir.kobj = kobj;
  582. /* link in */
  583. sysfs_addrm_start(&acxt, parent_sd);
  584. rc = sysfs_add_one(&acxt, sd);
  585. sysfs_addrm_finish(&acxt);
  586. if (rc == 0)
  587. *p_sd = sd;
  588. else
  589. sysfs_put(sd);
  590. return rc;
  591. }
  592. int sysfs_create_subdir(struct kobject *kobj, const char *name,
  593. struct sysfs_dirent **p_sd)
  594. {
  595. return create_dir(kobj, kobj->sd, name, NULL, p_sd);
  596. }
  597. /**
  598. * sysfs_create_dir_ns - create a directory for an object with a namespace tag
  599. * @kobj: object we're creating directory for
  600. * @ns: the namespace tag to use
  601. */
  602. int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
  603. {
  604. struct sysfs_dirent *parent_sd, *sd;
  605. int error = 0;
  606. BUG_ON(!kobj);
  607. if (kobj->parent)
  608. parent_sd = kobj->parent->sd;
  609. else
  610. parent_sd = &sysfs_root;
  611. if (!parent_sd)
  612. return -ENOENT;
  613. error = create_dir(kobj, parent_sd, kobject_name(kobj), ns, &sd);
  614. if (!error)
  615. kobj->sd = sd;
  616. return error;
  617. }
  618. static struct dentry *sysfs_lookup(struct inode *dir, struct dentry *dentry,
  619. unsigned int flags)
  620. {
  621. struct dentry *ret = NULL;
  622. struct dentry *parent = dentry->d_parent;
  623. struct sysfs_dirent *parent_sd = parent->d_fsdata;
  624. struct sysfs_dirent *sd;
  625. struct inode *inode;
  626. const void *ns = NULL;
  627. mutex_lock(&sysfs_mutex);
  628. if (parent_sd->s_flags & SYSFS_FLAG_HAS_NS)
  629. ns = sysfs_info(dir->i_sb)->ns;
  630. sd = sysfs_find_dirent(parent_sd, dentry->d_name.name, ns);
  631. /* no such entry */
  632. if (!sd) {
  633. ret = ERR_PTR(-ENOENT);
  634. goto out_unlock;
  635. }
  636. dentry->d_fsdata = sysfs_get(sd);
  637. /* attach dentry and inode */
  638. inode = sysfs_get_inode(dir->i_sb, sd);
  639. if (!inode) {
  640. ret = ERR_PTR(-ENOMEM);
  641. goto out_unlock;
  642. }
  643. /* instantiate and hash dentry */
  644. ret = d_materialise_unique(dentry, inode);
  645. out_unlock:
  646. mutex_unlock(&sysfs_mutex);
  647. return ret;
  648. }
  649. const struct inode_operations sysfs_dir_inode_operations = {
  650. .lookup = sysfs_lookup,
  651. .permission = sysfs_permission,
  652. .setattr = sysfs_setattr,
  653. .getattr = sysfs_getattr,
  654. .setxattr = sysfs_setxattr,
  655. };
  656. static void remove_dir(struct sysfs_dirent *sd)
  657. {
  658. struct sysfs_addrm_cxt acxt;
  659. sysfs_addrm_start(&acxt, sd->s_parent);
  660. sysfs_remove_one(&acxt, sd);
  661. sysfs_addrm_finish(&acxt);
  662. }
  663. void sysfs_remove_subdir(struct sysfs_dirent *sd)
  664. {
  665. remove_dir(sd);
  666. }
  667. static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
  668. {
  669. struct sysfs_addrm_cxt acxt;
  670. struct rb_node *pos;
  671. if (!dir_sd)
  672. return;
  673. pr_debug("sysfs %s: removing dir\n", dir_sd->s_name);
  674. sysfs_addrm_start(&acxt, dir_sd);
  675. pos = rb_first(&dir_sd->s_dir.children);
  676. while (pos) {
  677. struct sysfs_dirent *sd = to_sysfs_dirent(pos);
  678. pos = rb_next(pos);
  679. if (sysfs_type(sd) != SYSFS_DIR)
  680. sysfs_remove_one(&acxt, sd);
  681. }
  682. sysfs_addrm_finish(&acxt);
  683. remove_dir(dir_sd);
  684. }
  685. /**
  686. * sysfs_remove_dir - remove an object's directory.
  687. * @kobj: object.
  688. *
  689. * The only thing special about this is that we remove any files in
  690. * the directory before we remove the directory, and we've inlined
  691. * what used to be sysfs_rmdir() below, instead of calling separately.
  692. */
  693. void sysfs_remove_dir(struct kobject *kobj)
  694. {
  695. struct sysfs_dirent *sd = kobj->sd;
  696. spin_lock(&sysfs_assoc_lock);
  697. kobj->sd = NULL;
  698. spin_unlock(&sysfs_assoc_lock);
  699. __sysfs_remove_dir(sd);
  700. }
  701. int sysfs_rename(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent_sd,
  702. const char *new_name, const void *new_ns)
  703. {
  704. int error;
  705. mutex_lock(&sysfs_mutex);
  706. error = 0;
  707. if ((sd->s_parent == new_parent_sd) && (sd->s_ns == new_ns) &&
  708. (strcmp(sd->s_name, new_name) == 0))
  709. goto out; /* nothing to rename */
  710. error = -EEXIST;
  711. if (sysfs_find_dirent(new_parent_sd, new_name, new_ns))
  712. goto out;
  713. /* rename sysfs_dirent */
  714. if (strcmp(sd->s_name, new_name) != 0) {
  715. error = -ENOMEM;
  716. new_name = kstrdup(new_name, GFP_KERNEL);
  717. if (!new_name)
  718. goto out;
  719. kfree(sd->s_name);
  720. sd->s_name = new_name;
  721. }
  722. /*
  723. * Move to the appropriate place in the appropriate directories rbtree.
  724. */
  725. sysfs_unlink_sibling(sd);
  726. sysfs_get(new_parent_sd);
  727. sysfs_put(sd->s_parent);
  728. sd->s_ns = new_ns;
  729. sd->s_hash = sysfs_name_hash(sd->s_name, sd->s_ns);
  730. sd->s_parent = new_parent_sd;
  731. sysfs_link_sibling(sd);
  732. error = 0;
  733. out:
  734. mutex_unlock(&sysfs_mutex);
  735. return error;
  736. }
  737. int sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
  738. const void *new_ns)
  739. {
  740. struct sysfs_dirent *parent_sd = kobj->sd->s_parent;
  741. return sysfs_rename(kobj->sd, parent_sd, new_name, new_ns);
  742. }
  743. int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj,
  744. const void *new_ns)
  745. {
  746. struct sysfs_dirent *sd = kobj->sd;
  747. struct sysfs_dirent *new_parent_sd;
  748. BUG_ON(!sd->s_parent);
  749. new_parent_sd = new_parent_kobj && new_parent_kobj->sd ?
  750. new_parent_kobj->sd : &sysfs_root;
  751. return sysfs_rename(sd, new_parent_sd, sd->s_name, new_ns);
  752. }
  753. /* Relationship between s_mode and the DT_xxx types */
  754. static inline unsigned char dt_type(struct sysfs_dirent *sd)
  755. {
  756. return (sd->s_mode >> 12) & 15;
  757. }
  758. static int sysfs_dir_release(struct inode *inode, struct file *filp)
  759. {
  760. sysfs_put(filp->private_data);
  761. return 0;
  762. }
  763. static struct sysfs_dirent *sysfs_dir_pos(const void *ns,
  764. struct sysfs_dirent *parent_sd, loff_t hash, struct sysfs_dirent *pos)
  765. {
  766. if (pos) {
  767. int valid = !(pos->s_flags & SYSFS_FLAG_REMOVED) &&
  768. pos->s_parent == parent_sd &&
  769. hash == pos->s_hash;
  770. sysfs_put(pos);
  771. if (!valid)
  772. pos = NULL;
  773. }
  774. if (!pos && (hash > 1) && (hash < INT_MAX)) {
  775. struct rb_node *node = parent_sd->s_dir.children.rb_node;
  776. while (node) {
  777. pos = to_sysfs_dirent(node);
  778. if (hash < pos->s_hash)
  779. node = node->rb_left;
  780. else if (hash > pos->s_hash)
  781. node = node->rb_right;
  782. else
  783. break;
  784. }
  785. }
  786. /* Skip over entries in the wrong namespace */
  787. while (pos && pos->s_ns != ns) {
  788. struct rb_node *node = rb_next(&pos->s_rb);
  789. if (!node)
  790. pos = NULL;
  791. else
  792. pos = to_sysfs_dirent(node);
  793. }
  794. return pos;
  795. }
  796. static struct sysfs_dirent *sysfs_dir_next_pos(const void *ns,
  797. struct sysfs_dirent *parent_sd, ino_t ino, struct sysfs_dirent *pos)
  798. {
  799. pos = sysfs_dir_pos(ns, parent_sd, ino, pos);
  800. if (pos)
  801. do {
  802. struct rb_node *node = rb_next(&pos->s_rb);
  803. if (!node)
  804. pos = NULL;
  805. else
  806. pos = to_sysfs_dirent(node);
  807. } while (pos && pos->s_ns != ns);
  808. return pos;
  809. }
  810. static int sysfs_readdir(struct file *file, struct dir_context *ctx)
  811. {
  812. struct dentry *dentry = file->f_path.dentry;
  813. struct sysfs_dirent *parent_sd = dentry->d_fsdata;
  814. struct sysfs_dirent *pos = file->private_data;
  815. const void *ns = NULL;
  816. if (!dir_emit_dots(file, ctx))
  817. return 0;
  818. mutex_lock(&sysfs_mutex);
  819. if (parent_sd->s_flags & SYSFS_FLAG_HAS_NS)
  820. ns = sysfs_info(dentry->d_sb)->ns;
  821. for (pos = sysfs_dir_pos(ns, parent_sd, ctx->pos, pos);
  822. pos;
  823. pos = sysfs_dir_next_pos(ns, parent_sd, ctx->pos, pos)) {
  824. const char *name = pos->s_name;
  825. unsigned int type = dt_type(pos);
  826. int len = strlen(name);
  827. ino_t ino = pos->s_ino;
  828. ctx->pos = pos->s_hash;
  829. file->private_data = sysfs_get(pos);
  830. mutex_unlock(&sysfs_mutex);
  831. if (!dir_emit(ctx, name, len, ino, type))
  832. return 0;
  833. mutex_lock(&sysfs_mutex);
  834. }
  835. mutex_unlock(&sysfs_mutex);
  836. file->private_data = NULL;
  837. ctx->pos = INT_MAX;
  838. return 0;
  839. }
  840. static loff_t sysfs_dir_llseek(struct file *file, loff_t offset, int whence)
  841. {
  842. struct inode *inode = file_inode(file);
  843. loff_t ret;
  844. mutex_lock(&inode->i_mutex);
  845. ret = generic_file_llseek(file, offset, whence);
  846. mutex_unlock(&inode->i_mutex);
  847. return ret;
  848. }
  849. const struct file_operations sysfs_dir_operations = {
  850. .read = generic_read_dir,
  851. .iterate = sysfs_readdir,
  852. .release = sysfs_dir_release,
  853. .llseek = sysfs_dir_llseek,
  854. };