dir.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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. /*
  472. * Removal can be called multiple times on the same node. Only the
  473. * first invocation is effective and puts the base ref.
  474. */
  475. if (sd->s_flags & SYSFS_FLAG_REMOVED)
  476. return;
  477. sysfs_unlink_sibling(sd);
  478. /* Update timestamps on the parent */
  479. ps_iattr = sd->s_parent->s_iattr;
  480. if (ps_iattr) {
  481. struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
  482. ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
  483. }
  484. sd->s_flags |= SYSFS_FLAG_REMOVED;
  485. sd->u.removed_list = acxt->removed;
  486. acxt->removed = sd;
  487. }
  488. /**
  489. * sysfs_addrm_finish - finish up sysfs_dirent add/remove
  490. * @acxt: addrm context to finish up
  491. *
  492. * Finish up sysfs_dirent add/remove. Resources acquired by
  493. * sysfs_addrm_start() are released and removed sysfs_dirents are
  494. * cleaned up.
  495. *
  496. * LOCKING:
  497. * sysfs_mutex is released.
  498. */
  499. void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
  500. __releases(sysfs_mutex)
  501. {
  502. /* release resources acquired by sysfs_addrm_start() */
  503. mutex_unlock(&sysfs_mutex);
  504. /* kill removed sysfs_dirents */
  505. while (acxt->removed) {
  506. struct sysfs_dirent *sd = acxt->removed;
  507. acxt->removed = sd->u.removed_list;
  508. sysfs_deactivate(sd);
  509. unmap_bin_file(sd);
  510. sysfs_put(sd);
  511. }
  512. }
  513. /**
  514. * sysfs_find_dirent - find sysfs_dirent with the given name
  515. * @parent_sd: sysfs_dirent to search under
  516. * @name: name to look for
  517. * @ns: the namespace tag to use
  518. *
  519. * Look for sysfs_dirent with name @name under @parent_sd.
  520. *
  521. * LOCKING:
  522. * mutex_lock(sysfs_mutex)
  523. *
  524. * RETURNS:
  525. * Pointer to sysfs_dirent if found, NULL if not.
  526. */
  527. struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
  528. const unsigned char *name,
  529. const void *ns)
  530. {
  531. struct rb_node *node = parent_sd->s_dir.children.rb_node;
  532. unsigned int hash;
  533. hash = sysfs_name_hash(name, ns);
  534. while (node) {
  535. struct sysfs_dirent *sd;
  536. int result;
  537. sd = to_sysfs_dirent(node);
  538. result = sysfs_name_compare(hash, name, ns, sd);
  539. if (result < 0)
  540. node = node->rb_left;
  541. else if (result > 0)
  542. node = node->rb_right;
  543. else
  544. return sd;
  545. }
  546. return NULL;
  547. }
  548. /**
  549. * sysfs_get_dirent_ns - find and get sysfs_dirent with the given name
  550. * @parent_sd: sysfs_dirent to search under
  551. * @name: name to look for
  552. * @ns: the namespace tag to use
  553. *
  554. * Look for sysfs_dirent with name @name under @parent_sd and get
  555. * it if found.
  556. *
  557. * LOCKING:
  558. * Kernel thread context (may sleep). Grabs sysfs_mutex.
  559. *
  560. * RETURNS:
  561. * Pointer to sysfs_dirent if found, NULL if not.
  562. */
  563. struct sysfs_dirent *sysfs_get_dirent_ns(struct sysfs_dirent *parent_sd,
  564. const unsigned char *name,
  565. const void *ns)
  566. {
  567. struct sysfs_dirent *sd;
  568. mutex_lock(&sysfs_mutex);
  569. sd = sysfs_find_dirent(parent_sd, name, ns);
  570. sysfs_get(sd);
  571. mutex_unlock(&sysfs_mutex);
  572. return sd;
  573. }
  574. EXPORT_SYMBOL_GPL(sysfs_get_dirent_ns);
  575. static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
  576. const char *name, const void *ns,
  577. struct sysfs_dirent **p_sd)
  578. {
  579. umode_t mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
  580. struct sysfs_addrm_cxt acxt;
  581. struct sysfs_dirent *sd;
  582. int rc;
  583. /* allocate */
  584. sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
  585. if (!sd)
  586. return -ENOMEM;
  587. sd->s_ns = ns;
  588. sd->s_dir.kobj = kobj;
  589. /* link in */
  590. sysfs_addrm_start(&acxt);
  591. rc = sysfs_add_one(&acxt, sd, parent_sd);
  592. sysfs_addrm_finish(&acxt);
  593. if (rc == 0)
  594. *p_sd = sd;
  595. else
  596. sysfs_put(sd);
  597. return rc;
  598. }
  599. int sysfs_create_subdir(struct kobject *kobj, const char *name,
  600. struct sysfs_dirent **p_sd)
  601. {
  602. return create_dir(kobj, kobj->sd, name, NULL, p_sd);
  603. }
  604. /**
  605. * sysfs_create_dir_ns - create a directory for an object with a namespace tag
  606. * @kobj: object we're creating directory for
  607. * @ns: the namespace tag to use
  608. */
  609. int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
  610. {
  611. struct sysfs_dirent *parent_sd, *sd;
  612. int error = 0;
  613. BUG_ON(!kobj);
  614. if (kobj->parent)
  615. parent_sd = kobj->parent->sd;
  616. else
  617. parent_sd = &sysfs_root;
  618. if (!parent_sd)
  619. return -ENOENT;
  620. error = create_dir(kobj, parent_sd, kobject_name(kobj), ns, &sd);
  621. if (!error)
  622. kobj->sd = sd;
  623. return error;
  624. }
  625. static struct dentry *sysfs_lookup(struct inode *dir, struct dentry *dentry,
  626. unsigned int flags)
  627. {
  628. struct dentry *ret = NULL;
  629. struct dentry *parent = dentry->d_parent;
  630. struct sysfs_dirent *parent_sd = parent->d_fsdata;
  631. struct sysfs_dirent *sd;
  632. struct inode *inode;
  633. const void *ns = NULL;
  634. mutex_lock(&sysfs_mutex);
  635. if (parent_sd->s_flags & SYSFS_FLAG_HAS_NS)
  636. ns = sysfs_info(dir->i_sb)->ns;
  637. sd = sysfs_find_dirent(parent_sd, dentry->d_name.name, ns);
  638. /* no such entry */
  639. if (!sd) {
  640. ret = ERR_PTR(-ENOENT);
  641. goto out_unlock;
  642. }
  643. dentry->d_fsdata = sysfs_get(sd);
  644. /* attach dentry and inode */
  645. inode = sysfs_get_inode(dir->i_sb, sd);
  646. if (!inode) {
  647. ret = ERR_PTR(-ENOMEM);
  648. goto out_unlock;
  649. }
  650. /* instantiate and hash dentry */
  651. ret = d_materialise_unique(dentry, inode);
  652. out_unlock:
  653. mutex_unlock(&sysfs_mutex);
  654. return ret;
  655. }
  656. const struct inode_operations sysfs_dir_inode_operations = {
  657. .lookup = sysfs_lookup,
  658. .permission = sysfs_permission,
  659. .setattr = sysfs_setattr,
  660. .getattr = sysfs_getattr,
  661. .setxattr = sysfs_setxattr,
  662. };
  663. static void remove_dir(struct sysfs_dirent *sd)
  664. {
  665. struct sysfs_addrm_cxt acxt;
  666. sysfs_addrm_start(&acxt);
  667. sysfs_remove_one(&acxt, sd);
  668. sysfs_addrm_finish(&acxt);
  669. }
  670. void sysfs_remove_subdir(struct sysfs_dirent *sd)
  671. {
  672. remove_dir(sd);
  673. }
  674. static struct sysfs_dirent *sysfs_leftmost_descendant(struct sysfs_dirent *pos)
  675. {
  676. struct sysfs_dirent *last;
  677. while (true) {
  678. struct rb_node *rbn;
  679. last = pos;
  680. if (sysfs_type(pos) != SYSFS_DIR)
  681. break;
  682. rbn = rb_first(&pos->s_dir.children);
  683. if (!rbn)
  684. break;
  685. pos = to_sysfs_dirent(rbn);
  686. }
  687. return last;
  688. }
  689. /**
  690. * sysfs_next_descendant_post - find the next descendant for post-order walk
  691. * @pos: the current position (%NULL to initiate traversal)
  692. * @root: sysfs_dirent whose descendants to walk
  693. *
  694. * Find the next descendant to visit for post-order traversal of @root's
  695. * descendants. @root is included in the iteration and the last node to be
  696. * visited.
  697. */
  698. static struct sysfs_dirent *sysfs_next_descendant_post(struct sysfs_dirent *pos,
  699. struct sysfs_dirent *root)
  700. {
  701. struct rb_node *rbn;
  702. lockdep_assert_held(&sysfs_mutex);
  703. /* if first iteration, visit leftmost descendant which may be root */
  704. if (!pos)
  705. return sysfs_leftmost_descendant(root);
  706. /* if we visited @root, we're done */
  707. if (pos == root)
  708. return NULL;
  709. /* if there's an unvisited sibling, visit its leftmost descendant */
  710. rbn = rb_next(&pos->s_rb);
  711. if (rbn)
  712. return sysfs_leftmost_descendant(to_sysfs_dirent(rbn));
  713. /* no sibling left, visit parent */
  714. return pos->s_parent;
  715. }
  716. static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
  717. {
  718. struct sysfs_addrm_cxt acxt;
  719. struct sysfs_dirent *pos, *next;
  720. if (!dir_sd)
  721. return;
  722. pr_debug("sysfs %s: removing dir\n", dir_sd->s_name);
  723. sysfs_addrm_start(&acxt);
  724. next = NULL;
  725. do {
  726. pos = next;
  727. next = sysfs_next_descendant_post(pos, dir_sd);
  728. if (pos)
  729. sysfs_remove_one(&acxt, pos);
  730. } while (next);
  731. sysfs_addrm_finish(&acxt);
  732. }
  733. /**
  734. * sysfs_remove_dir - remove an object's directory.
  735. * @kobj: object.
  736. *
  737. * The only thing special about this is that we remove any files in
  738. * the directory before we remove the directory, and we've inlined
  739. * what used to be sysfs_rmdir() below, instead of calling separately.
  740. */
  741. void sysfs_remove_dir(struct kobject *kobj)
  742. {
  743. struct sysfs_dirent *sd = kobj->sd;
  744. spin_lock(&sysfs_assoc_lock);
  745. kobj->sd = NULL;
  746. spin_unlock(&sysfs_assoc_lock);
  747. __sysfs_remove_dir(sd);
  748. }
  749. int sysfs_rename(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent_sd,
  750. const char *new_name, const void *new_ns)
  751. {
  752. int error;
  753. mutex_lock(&sysfs_mutex);
  754. error = 0;
  755. if ((sd->s_parent == new_parent_sd) && (sd->s_ns == new_ns) &&
  756. (strcmp(sd->s_name, new_name) == 0))
  757. goto out; /* nothing to rename */
  758. error = -EEXIST;
  759. if (sysfs_find_dirent(new_parent_sd, new_name, new_ns))
  760. goto out;
  761. /* rename sysfs_dirent */
  762. if (strcmp(sd->s_name, new_name) != 0) {
  763. error = -ENOMEM;
  764. new_name = kstrdup(new_name, GFP_KERNEL);
  765. if (!new_name)
  766. goto out;
  767. kfree(sd->s_name);
  768. sd->s_name = new_name;
  769. }
  770. /*
  771. * Move to the appropriate place in the appropriate directories rbtree.
  772. */
  773. sysfs_unlink_sibling(sd);
  774. sysfs_get(new_parent_sd);
  775. sysfs_put(sd->s_parent);
  776. sd->s_ns = new_ns;
  777. sd->s_hash = sysfs_name_hash(sd->s_name, sd->s_ns);
  778. sd->s_parent = new_parent_sd;
  779. sysfs_link_sibling(sd);
  780. error = 0;
  781. out:
  782. mutex_unlock(&sysfs_mutex);
  783. return error;
  784. }
  785. int sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
  786. const void *new_ns)
  787. {
  788. struct sysfs_dirent *parent_sd = kobj->sd->s_parent;
  789. return sysfs_rename(kobj->sd, parent_sd, new_name, new_ns);
  790. }
  791. int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj,
  792. const void *new_ns)
  793. {
  794. struct sysfs_dirent *sd = kobj->sd;
  795. struct sysfs_dirent *new_parent_sd;
  796. BUG_ON(!sd->s_parent);
  797. new_parent_sd = new_parent_kobj && new_parent_kobj->sd ?
  798. new_parent_kobj->sd : &sysfs_root;
  799. return sysfs_rename(sd, new_parent_sd, sd->s_name, new_ns);
  800. }
  801. /* Relationship between s_mode and the DT_xxx types */
  802. static inline unsigned char dt_type(struct sysfs_dirent *sd)
  803. {
  804. return (sd->s_mode >> 12) & 15;
  805. }
  806. static int sysfs_dir_release(struct inode *inode, struct file *filp)
  807. {
  808. sysfs_put(filp->private_data);
  809. return 0;
  810. }
  811. static struct sysfs_dirent *sysfs_dir_pos(const void *ns,
  812. struct sysfs_dirent *parent_sd, loff_t hash, struct sysfs_dirent *pos)
  813. {
  814. if (pos) {
  815. int valid = !(pos->s_flags & SYSFS_FLAG_REMOVED) &&
  816. pos->s_parent == parent_sd &&
  817. hash == pos->s_hash;
  818. sysfs_put(pos);
  819. if (!valid)
  820. pos = NULL;
  821. }
  822. if (!pos && (hash > 1) && (hash < INT_MAX)) {
  823. struct rb_node *node = parent_sd->s_dir.children.rb_node;
  824. while (node) {
  825. pos = to_sysfs_dirent(node);
  826. if (hash < pos->s_hash)
  827. node = node->rb_left;
  828. else if (hash > pos->s_hash)
  829. node = node->rb_right;
  830. else
  831. break;
  832. }
  833. }
  834. /* Skip over entries in the wrong namespace */
  835. while (pos && pos->s_ns != ns) {
  836. struct rb_node *node = rb_next(&pos->s_rb);
  837. if (!node)
  838. pos = NULL;
  839. else
  840. pos = to_sysfs_dirent(node);
  841. }
  842. return pos;
  843. }
  844. static struct sysfs_dirent *sysfs_dir_next_pos(const void *ns,
  845. struct sysfs_dirent *parent_sd, ino_t ino, struct sysfs_dirent *pos)
  846. {
  847. pos = sysfs_dir_pos(ns, parent_sd, ino, pos);
  848. if (pos)
  849. do {
  850. struct rb_node *node = rb_next(&pos->s_rb);
  851. if (!node)
  852. pos = NULL;
  853. else
  854. pos = to_sysfs_dirent(node);
  855. } while (pos && pos->s_ns != ns);
  856. return pos;
  857. }
  858. static int sysfs_readdir(struct file *file, struct dir_context *ctx)
  859. {
  860. struct dentry *dentry = file->f_path.dentry;
  861. struct sysfs_dirent *parent_sd = dentry->d_fsdata;
  862. struct sysfs_dirent *pos = file->private_data;
  863. const void *ns = NULL;
  864. if (!dir_emit_dots(file, ctx))
  865. return 0;
  866. mutex_lock(&sysfs_mutex);
  867. if (parent_sd->s_flags & SYSFS_FLAG_HAS_NS)
  868. ns = sysfs_info(dentry->d_sb)->ns;
  869. for (pos = sysfs_dir_pos(ns, parent_sd, ctx->pos, pos);
  870. pos;
  871. pos = sysfs_dir_next_pos(ns, parent_sd, ctx->pos, pos)) {
  872. const char *name = pos->s_name;
  873. unsigned int type = dt_type(pos);
  874. int len = strlen(name);
  875. ino_t ino = pos->s_ino;
  876. ctx->pos = pos->s_hash;
  877. file->private_data = sysfs_get(pos);
  878. mutex_unlock(&sysfs_mutex);
  879. if (!dir_emit(ctx, name, len, ino, type))
  880. return 0;
  881. mutex_lock(&sysfs_mutex);
  882. }
  883. mutex_unlock(&sysfs_mutex);
  884. file->private_data = NULL;
  885. ctx->pos = INT_MAX;
  886. return 0;
  887. }
  888. static loff_t sysfs_dir_llseek(struct file *file, loff_t offset, int whence)
  889. {
  890. struct inode *inode = file_inode(file);
  891. loff_t ret;
  892. mutex_lock(&inode->i_mutex);
  893. ret = generic_file_llseek(file, offset, whence);
  894. mutex_unlock(&inode->i_mutex);
  895. return ret;
  896. }
  897. const struct file_operations sysfs_dir_operations = {
  898. .read = generic_read_dir,
  899. .iterate = sysfs_readdir,
  900. .release = sysfs_dir_release,
  901. .llseek = sysfs_dir_llseek,
  902. };