dir.c 25 KB

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