dir.c 26 KB

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