dir.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  1. /*
  2. * fs/sysfs/dir.c - sysfs core and dir operation implementation
  3. *
  4. * Copyright (c) 2001-3 Patrick Mochel
  5. * Copyright (c) 2007 SUSE Linux Products GmbH
  6. * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
  7. *
  8. * This file is released under the GPLv2.
  9. *
  10. * Please see Documentation/filesystems/sysfs.txt for more information.
  11. */
  12. #undef DEBUG
  13. #include <linux/fs.h>
  14. #include <linux/mount.h>
  15. #include <linux/module.h>
  16. #include <linux/kobject.h>
  17. #include <linux/namei.h>
  18. #include <linux/idr.h>
  19. #include <linux/completion.h>
  20. #include <linux/mutex.h>
  21. #include <linux/slab.h>
  22. #include <linux/security.h>
  23. #include <linux/hash.h>
  24. #include "sysfs.h"
  25. DEFINE_MUTEX(sysfs_mutex);
  26. DEFINE_SPINLOCK(sysfs_symlink_target_lock);
  27. #define to_sysfs_dirent(X) rb_entry((X), struct sysfs_dirent, s_rb)
  28. static DEFINE_SPINLOCK(sysfs_ino_lock);
  29. static DEFINE_IDA(sysfs_ino_ida);
  30. /**
  31. * sysfs_name_hash
  32. * @name: Null terminated string to hash
  33. * @ns: Namespace tag to hash
  34. *
  35. * Returns 31 bit hash of ns + name (so it fits in an off_t )
  36. */
  37. static unsigned int sysfs_name_hash(const char *name, const void *ns)
  38. {
  39. unsigned long hash = init_name_hash();
  40. unsigned int len = strlen(name);
  41. while (len--)
  42. hash = partial_name_hash(*name++, hash);
  43. hash = (end_name_hash(hash) ^ hash_ptr((void *)ns, 31));
  44. hash &= 0x7fffffffU;
  45. /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */
  46. if (hash < 1)
  47. hash += 2;
  48. if (hash >= INT_MAX)
  49. hash = INT_MAX - 1;
  50. return hash;
  51. }
  52. static int sysfs_name_compare(unsigned int hash, const char *name,
  53. const void *ns, const struct sysfs_dirent *sd)
  54. {
  55. if (hash != sd->s_hash)
  56. return hash - sd->s_hash;
  57. if (ns != sd->s_ns)
  58. return ns - sd->s_ns;
  59. return strcmp(name, sd->s_name);
  60. }
  61. static int sysfs_sd_compare(const struct sysfs_dirent *left,
  62. const struct sysfs_dirent *right)
  63. {
  64. return sysfs_name_compare(left->s_hash, left->s_name, left->s_ns,
  65. right);
  66. }
  67. /**
  68. * sysfs_link_sibling - link sysfs_dirent into sibling rbtree
  69. * @sd: sysfs_dirent of interest
  70. *
  71. * Link @sd into its sibling rbtree which starts from
  72. * sd->s_parent->s_dir.children.
  73. *
  74. * Locking:
  75. * mutex_lock(sysfs_mutex)
  76. *
  77. * RETURNS:
  78. * 0 on susccess -EEXIST on failure.
  79. */
  80. static int sysfs_link_sibling(struct sysfs_dirent *sd)
  81. {
  82. struct rb_node **node = &sd->s_parent->s_dir.children.rb_node;
  83. struct rb_node *parent = NULL;
  84. if (sysfs_type(sd) == SYSFS_DIR)
  85. sd->s_parent->s_dir.subdirs++;
  86. while (*node) {
  87. struct sysfs_dirent *pos;
  88. int result;
  89. pos = to_sysfs_dirent(*node);
  90. parent = *node;
  91. result = sysfs_sd_compare(sd, pos);
  92. if (result < 0)
  93. node = &pos->s_rb.rb_left;
  94. else if (result > 0)
  95. node = &pos->s_rb.rb_right;
  96. else
  97. return -EEXIST;
  98. }
  99. /* add new node and rebalance the tree */
  100. rb_link_node(&sd->s_rb, parent, node);
  101. rb_insert_color(&sd->s_rb, &sd->s_parent->s_dir.children);
  102. return 0;
  103. }
  104. /**
  105. * sysfs_unlink_sibling - unlink sysfs_dirent from sibling rbtree
  106. * @sd: sysfs_dirent of interest
  107. *
  108. * Unlink @sd from its sibling rbtree which starts from
  109. * sd->s_parent->s_dir.children.
  110. *
  111. * Locking:
  112. * mutex_lock(sysfs_mutex)
  113. */
  114. static void sysfs_unlink_sibling(struct sysfs_dirent *sd)
  115. {
  116. if (sysfs_type(sd) == SYSFS_DIR)
  117. sd->s_parent->s_dir.subdirs--;
  118. rb_erase(&sd->s_rb, &sd->s_parent->s_dir.children);
  119. }
  120. /**
  121. * sysfs_get_active - get an active reference to sysfs_dirent
  122. * @sd: sysfs_dirent to get an active reference to
  123. *
  124. * Get an active reference of @sd. This function is noop if @sd
  125. * is NULL.
  126. *
  127. * RETURNS:
  128. * Pointer to @sd on success, NULL on failure.
  129. */
  130. struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
  131. {
  132. if (unlikely(!sd))
  133. return NULL;
  134. if (!atomic_inc_unless_negative(&sd->s_active))
  135. return NULL;
  136. if (likely(!sysfs_ignore_lockdep(sd)))
  137. rwsem_acquire_read(&sd->dep_map, 0, 1, _RET_IP_);
  138. return sd;
  139. }
  140. /**
  141. * sysfs_put_active - put an active reference to sysfs_dirent
  142. * @sd: sysfs_dirent to put an active reference to
  143. *
  144. * Put an active reference to @sd. This function is noop if @sd
  145. * is NULL.
  146. */
  147. void sysfs_put_active(struct sysfs_dirent *sd)
  148. {
  149. int v;
  150. if (unlikely(!sd))
  151. return;
  152. if (likely(!sysfs_ignore_lockdep(sd)))
  153. rwsem_release(&sd->dep_map, 1, _RET_IP_);
  154. v = atomic_dec_return(&sd->s_active);
  155. if (likely(v != SD_DEACTIVATED_BIAS))
  156. return;
  157. /* atomic_dec_return() is a mb(), we'll always see the updated
  158. * sd->u.completion.
  159. */
  160. complete(sd->u.completion);
  161. }
  162. /**
  163. * sysfs_deactivate - deactivate sysfs_dirent
  164. * @sd: sysfs_dirent to deactivate
  165. *
  166. * Deny new active references and drain existing ones.
  167. */
  168. static void sysfs_deactivate(struct sysfs_dirent *sd)
  169. {
  170. DECLARE_COMPLETION_ONSTACK(wait);
  171. int v;
  172. BUG_ON(!(sd->s_flags & SYSFS_FLAG_REMOVED));
  173. if (!(sysfs_type(sd) & SYSFS_ACTIVE_REF))
  174. return;
  175. sd->u.completion = (void *)&wait;
  176. rwsem_acquire(&sd->dep_map, 0, 0, _RET_IP_);
  177. /* atomic_add_return() is a mb(), put_active() will always see
  178. * the updated sd->u.completion.
  179. */
  180. v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
  181. if (v != SD_DEACTIVATED_BIAS) {
  182. lock_contended(&sd->dep_map, _RET_IP_);
  183. wait_for_completion(&wait);
  184. }
  185. lock_acquired(&sd->dep_map, _RET_IP_);
  186. rwsem_release(&sd->dep_map, 1, _RET_IP_);
  187. }
  188. static int sysfs_alloc_ino(unsigned int *pino)
  189. {
  190. int ino, rc;
  191. retry:
  192. spin_lock(&sysfs_ino_lock);
  193. rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
  194. spin_unlock(&sysfs_ino_lock);
  195. if (rc == -EAGAIN) {
  196. if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
  197. goto retry;
  198. rc = -ENOMEM;
  199. }
  200. *pino = ino;
  201. return rc;
  202. }
  203. static void sysfs_free_ino(unsigned int ino)
  204. {
  205. spin_lock(&sysfs_ino_lock);
  206. ida_remove(&sysfs_ino_ida, ino);
  207. spin_unlock(&sysfs_ino_lock);
  208. }
  209. void release_sysfs_dirent(struct sysfs_dirent *sd)
  210. {
  211. struct sysfs_dirent *parent_sd;
  212. repeat:
  213. /* Moving/renaming is always done while holding reference.
  214. * sd->s_parent won't change beneath us.
  215. */
  216. parent_sd = sd->s_parent;
  217. WARN(!(sd->s_flags & SYSFS_FLAG_REMOVED),
  218. "sysfs: free using entry: %s/%s\n",
  219. parent_sd ? parent_sd->s_name : "", sd->s_name);
  220. if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
  221. sysfs_put(sd->s_symlink.target_sd);
  222. if (sysfs_type(sd) & SYSFS_COPY_NAME)
  223. kfree(sd->s_name);
  224. if (sd->s_iattr && sd->s_iattr->ia_secdata)
  225. security_release_secctx(sd->s_iattr->ia_secdata,
  226. sd->s_iattr->ia_secdata_len);
  227. kfree(sd->s_iattr);
  228. sysfs_free_ino(sd->s_ino);
  229. kmem_cache_free(sysfs_dir_cachep, sd);
  230. sd = parent_sd;
  231. if (sd && atomic_dec_and_test(&sd->s_count))
  232. goto repeat;
  233. }
  234. static int sysfs_dentry_delete(const struct dentry *dentry)
  235. {
  236. struct sysfs_dirent *sd = dentry->d_fsdata;
  237. return !(sd && !(sd->s_flags & SYSFS_FLAG_REMOVED));
  238. }
  239. static int sysfs_dentry_revalidate(struct dentry *dentry, unsigned int flags)
  240. {
  241. struct sysfs_dirent *sd;
  242. int type;
  243. if (flags & LOOKUP_RCU)
  244. return -ECHILD;
  245. sd = dentry->d_fsdata;
  246. mutex_lock(&sysfs_mutex);
  247. /* The sysfs dirent has been deleted */
  248. if (sd->s_flags & SYSFS_FLAG_REMOVED)
  249. goto out_bad;
  250. /* The sysfs dirent has been moved? */
  251. if (dentry->d_parent->d_fsdata != sd->s_parent)
  252. goto out_bad;
  253. /* The sysfs dirent has been renamed */
  254. if (strcmp(dentry->d_name.name, sd->s_name) != 0)
  255. goto out_bad;
  256. /* The sysfs dirent has been moved to a different namespace */
  257. type = KOBJ_NS_TYPE_NONE;
  258. if (sd->s_parent) {
  259. type = sysfs_ns_type(sd->s_parent);
  260. if (type != KOBJ_NS_TYPE_NONE &&
  261. sysfs_info(dentry->d_sb)->ns[type] != sd->s_ns)
  262. goto out_bad;
  263. }
  264. mutex_unlock(&sysfs_mutex);
  265. out_valid:
  266. return 1;
  267. out_bad:
  268. /* Remove the dentry from the dcache hashes.
  269. * If this is a deleted dentry we use d_drop instead of d_delete
  270. * so sysfs doesn't need to cope with negative dentries.
  271. *
  272. * If this is a dentry that has simply been renamed we
  273. * use d_drop to remove it from the dcache lookup on its
  274. * old parent. If this dentry persists later when a lookup
  275. * is performed at its new name the dentry will be readded
  276. * to the dcache hashes.
  277. */
  278. mutex_unlock(&sysfs_mutex);
  279. /* If we have submounts we must allow the vfs caches
  280. * to lie about the state of the filesystem to prevent
  281. * leaks and other nasty things.
  282. */
  283. if (check_submounts_and_drop(dentry) != 0)
  284. goto out_valid;
  285. return 0;
  286. }
  287. static void sysfs_dentry_release(struct dentry *dentry)
  288. {
  289. sysfs_put(dentry->d_fsdata);
  290. }
  291. const struct dentry_operations sysfs_dentry_ops = {
  292. .d_revalidate = sysfs_dentry_revalidate,
  293. .d_delete = sysfs_dentry_delete,
  294. .d_release = sysfs_dentry_release,
  295. };
  296. struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
  297. {
  298. char *dup_name = NULL;
  299. struct sysfs_dirent *sd;
  300. if (type & SYSFS_COPY_NAME) {
  301. name = dup_name = kstrdup(name, GFP_KERNEL);
  302. if (!name)
  303. return NULL;
  304. }
  305. sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
  306. if (!sd)
  307. goto err_out1;
  308. if (sysfs_alloc_ino(&sd->s_ino))
  309. goto err_out2;
  310. atomic_set(&sd->s_count, 1);
  311. atomic_set(&sd->s_active, 0);
  312. sd->s_name = name;
  313. sd->s_mode = mode;
  314. sd->s_flags = type | SYSFS_FLAG_REMOVED;
  315. return sd;
  316. err_out2:
  317. kmem_cache_free(sysfs_dir_cachep, sd);
  318. err_out1:
  319. kfree(dup_name);
  320. return NULL;
  321. }
  322. /**
  323. * sysfs_addrm_start - prepare for sysfs_dirent add/remove
  324. * @acxt: pointer to sysfs_addrm_cxt to be used
  325. *
  326. * This function is called when the caller is about to add or remove
  327. * sysfs_dirent. This function acquires sysfs_mutex. @acxt is used
  328. * to keep and pass context to other addrm functions.
  329. *
  330. * LOCKING:
  331. * Kernel thread context (may sleep). sysfs_mutex is locked on
  332. * return.
  333. */
  334. void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt)
  335. __acquires(sysfs_mutex)
  336. {
  337. memset(acxt, 0, sizeof(*acxt));
  338. mutex_lock(&sysfs_mutex);
  339. }
  340. /**
  341. * __sysfs_add_one - add sysfs_dirent to parent without warning
  342. * @acxt: addrm context to use
  343. * @sd: sysfs_dirent to be added
  344. * @parent_sd: the parent sysfs_dirent to add @sd to
  345. *
  346. * Get @parent_sd and set @sd->s_parent to it and increment nlink of
  347. * the parent inode if @sd is a directory and link into the children
  348. * list of the parent.
  349. *
  350. * This function should be called between calls to
  351. * sysfs_addrm_start() and sysfs_addrm_finish() and should be
  352. * passed the same @acxt as passed to sysfs_addrm_start().
  353. *
  354. * LOCKING:
  355. * Determined by sysfs_addrm_start().
  356. *
  357. * RETURNS:
  358. * 0 on success, -EEXIST if entry with the given name already
  359. * exists.
  360. */
  361. int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd,
  362. struct sysfs_dirent *parent_sd)
  363. {
  364. struct sysfs_inode_attrs *ps_iattr;
  365. int ret;
  366. if (!!sysfs_ns_type(parent_sd) != !!sd->s_ns) {
  367. WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n",
  368. sysfs_ns_type(parent_sd) ? "required" : "invalid",
  369. parent_sd->s_name, sd->s_name);
  370. return -EINVAL;
  371. }
  372. sd->s_hash = sysfs_name_hash(sd->s_name, sd->s_ns);
  373. sd->s_parent = sysfs_get(parent_sd);
  374. ret = sysfs_link_sibling(sd);
  375. if (ret)
  376. return ret;
  377. /* Update timestamps on the parent */
  378. ps_iattr = parent_sd->s_iattr;
  379. if (ps_iattr) {
  380. struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
  381. ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
  382. }
  383. /* Mark the entry added into directory tree */
  384. sd->s_flags &= ~SYSFS_FLAG_REMOVED;
  385. return 0;
  386. }
  387. /**
  388. * sysfs_pathname - return full path to sysfs dirent
  389. * @sd: sysfs_dirent whose path we want
  390. * @path: caller allocated buffer of size PATH_MAX
  391. *
  392. * Gives the name "/" to the sysfs_root entry; any path returned
  393. * is relative to wherever sysfs is mounted.
  394. */
  395. static char *sysfs_pathname(struct sysfs_dirent *sd, char *path)
  396. {
  397. if (sd->s_parent) {
  398. sysfs_pathname(sd->s_parent, path);
  399. strlcat(path, "/", PATH_MAX);
  400. }
  401. strlcat(path, sd->s_name, PATH_MAX);
  402. return path;
  403. }
  404. void sysfs_warn_dup(struct sysfs_dirent *parent, const char *name)
  405. {
  406. char *path;
  407. path = kzalloc(PATH_MAX, GFP_KERNEL);
  408. if (path) {
  409. sysfs_pathname(parent, path);
  410. strlcat(path, "/", PATH_MAX);
  411. strlcat(path, name, PATH_MAX);
  412. }
  413. WARN(1, KERN_WARNING "sysfs: cannot create duplicate filename '%s'\n",
  414. path ? path : name);
  415. kfree(path);
  416. }
  417. /**
  418. * sysfs_add_one - add sysfs_dirent to parent
  419. * @acxt: addrm context to use
  420. * @sd: sysfs_dirent to be added
  421. * @parent_sd: the parent sysfs_dirent to add @sd to
  422. *
  423. * Get @parent_sd and set @sd->s_parent to it and increment nlink of
  424. * the parent inode if @sd is a directory and link into the children
  425. * list of the parent.
  426. *
  427. * This function should be called between calls to
  428. * sysfs_addrm_start() and sysfs_addrm_finish() and should be
  429. * passed the same @acxt as passed to sysfs_addrm_start().
  430. *
  431. * LOCKING:
  432. * Determined by sysfs_addrm_start().
  433. *
  434. * RETURNS:
  435. * 0 on success, -EEXIST if entry with the given name already
  436. * exists.
  437. */
  438. int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd,
  439. struct sysfs_dirent *parent_sd)
  440. {
  441. int ret;
  442. ret = __sysfs_add_one(acxt, sd, parent_sd);
  443. if (ret == -EEXIST)
  444. sysfs_warn_dup(parent_sd, sd->s_name);
  445. return ret;
  446. }
  447. /**
  448. * sysfs_remove_one - remove sysfs_dirent from parent
  449. * @acxt: addrm context to use
  450. * @sd: sysfs_dirent to be removed
  451. *
  452. * Mark @sd removed and drop nlink of parent inode if @sd is a
  453. * directory. @sd is unlinked from the children list.
  454. *
  455. * This function should be called between calls to
  456. * sysfs_addrm_start() and sysfs_addrm_finish() and should be
  457. * passed the same @acxt as passed to sysfs_addrm_start().
  458. *
  459. * LOCKING:
  460. * Determined by sysfs_addrm_start().
  461. */
  462. static void sysfs_remove_one(struct sysfs_addrm_cxt *acxt,
  463. struct sysfs_dirent *sd)
  464. {
  465. struct sysfs_inode_attrs *ps_iattr;
  466. /*
  467. * Removal can be called multiple times on the same node. Only the
  468. * first invocation is effective and puts the base ref.
  469. */
  470. if (sd->s_flags & SYSFS_FLAG_REMOVED)
  471. return;
  472. sysfs_unlink_sibling(sd);
  473. /* Update timestamps on the parent */
  474. ps_iattr = sd->s_parent->s_iattr;
  475. if (ps_iattr) {
  476. struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
  477. ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
  478. }
  479. sd->s_flags |= SYSFS_FLAG_REMOVED;
  480. sd->u.removed_list = acxt->removed;
  481. acxt->removed = sd;
  482. }
  483. /**
  484. * sysfs_addrm_finish - finish up sysfs_dirent add/remove
  485. * @acxt: addrm context to finish up
  486. *
  487. * Finish up sysfs_dirent add/remove. Resources acquired by
  488. * sysfs_addrm_start() are released and removed sysfs_dirents are
  489. * cleaned up.
  490. *
  491. * LOCKING:
  492. * sysfs_mutex is released.
  493. */
  494. void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
  495. __releases(sysfs_mutex)
  496. {
  497. /* release resources acquired by sysfs_addrm_start() */
  498. mutex_unlock(&sysfs_mutex);
  499. /* kill removed sysfs_dirents */
  500. while (acxt->removed) {
  501. struct sysfs_dirent *sd = acxt->removed;
  502. acxt->removed = sd->u.removed_list;
  503. sysfs_deactivate(sd);
  504. sysfs_unmap_bin_file(sd);
  505. sysfs_put(sd);
  506. }
  507. }
  508. /**
  509. * sysfs_find_dirent - find sysfs_dirent with the given name
  510. * @parent_sd: sysfs_dirent to search under
  511. * @name: name to look for
  512. * @ns: the namespace tag to use
  513. *
  514. * Look for sysfs_dirent with name @name under @parent_sd.
  515. *
  516. * LOCKING:
  517. * mutex_lock(sysfs_mutex)
  518. *
  519. * RETURNS:
  520. * Pointer to sysfs_dirent if found, NULL if not.
  521. */
  522. struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
  523. const unsigned char *name,
  524. const void *ns)
  525. {
  526. struct rb_node *node = parent_sd->s_dir.children.rb_node;
  527. unsigned int hash;
  528. if (!!sysfs_ns_type(parent_sd) != !!ns) {
  529. WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n",
  530. sysfs_ns_type(parent_sd) ? "required" : "invalid",
  531. parent_sd->s_name, name);
  532. return NULL;
  533. }
  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. enum kobj_ns_type type,
  578. const char *name, const void *ns,
  579. struct sysfs_dirent **p_sd)
  580. {
  581. umode_t mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
  582. struct sysfs_addrm_cxt acxt;
  583. struct sysfs_dirent *sd;
  584. int rc;
  585. /* allocate */
  586. sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
  587. if (!sd)
  588. return -ENOMEM;
  589. sd->s_flags |= (type << SYSFS_NS_TYPE_SHIFT);
  590. sd->s_ns = ns;
  591. sd->s_dir.kobj = kobj;
  592. /* link in */
  593. sysfs_addrm_start(&acxt);
  594. rc = sysfs_add_one(&acxt, sd, parent_sd);
  595. sysfs_addrm_finish(&acxt);
  596. if (rc == 0)
  597. *p_sd = sd;
  598. else
  599. sysfs_put(sd);
  600. return rc;
  601. }
  602. int sysfs_create_subdir(struct kobject *kobj, const char *name,
  603. struct sysfs_dirent **p_sd)
  604. {
  605. return create_dir(kobj, kobj->sd,
  606. KOBJ_NS_TYPE_NONE, name, NULL, p_sd);
  607. }
  608. /**
  609. * sysfs_read_ns_type: return associated ns_type
  610. * @kobj: the kobject being queried
  611. *
  612. * Each kobject can be tagged with exactly one namespace type
  613. * (i.e. network or user). Return the ns_type associated with
  614. * this object if any
  615. */
  616. static enum kobj_ns_type sysfs_read_ns_type(struct kobject *kobj)
  617. {
  618. const struct kobj_ns_type_operations *ops;
  619. enum kobj_ns_type type;
  620. ops = kobj_child_ns_ops(kobj);
  621. if (!ops)
  622. return KOBJ_NS_TYPE_NONE;
  623. type = ops->type;
  624. BUG_ON(type <= KOBJ_NS_TYPE_NONE);
  625. BUG_ON(type >= KOBJ_NS_TYPES);
  626. BUG_ON(!kobj_ns_type_registered(type));
  627. return type;
  628. }
  629. /**
  630. * sysfs_create_dir_ns - create a directory for an object with a namespace tag
  631. * @kobj: object we're creating directory for
  632. * @ns: the namespace tag to use
  633. */
  634. int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
  635. {
  636. enum kobj_ns_type type;
  637. struct sysfs_dirent *parent_sd, *sd;
  638. int error = 0;
  639. BUG_ON(!kobj);
  640. if (kobj->parent)
  641. parent_sd = kobj->parent->sd;
  642. else
  643. parent_sd = &sysfs_root;
  644. if (!parent_sd)
  645. return -ENOENT;
  646. type = sysfs_read_ns_type(kobj);
  647. error = create_dir(kobj, parent_sd, type, kobject_name(kobj), ns, &sd);
  648. if (!error)
  649. kobj->sd = sd;
  650. return error;
  651. }
  652. static struct dentry *sysfs_lookup(struct inode *dir, struct dentry *dentry,
  653. unsigned int flags)
  654. {
  655. struct dentry *ret = NULL;
  656. struct dentry *parent = dentry->d_parent;
  657. struct sysfs_dirent *parent_sd = parent->d_fsdata;
  658. struct sysfs_dirent *sd;
  659. struct inode *inode;
  660. enum kobj_ns_type type;
  661. const void *ns;
  662. mutex_lock(&sysfs_mutex);
  663. type = sysfs_ns_type(parent_sd);
  664. ns = sysfs_info(dir->i_sb)->ns[type];
  665. sd = sysfs_find_dirent(parent_sd, dentry->d_name.name, ns);
  666. /* no such entry */
  667. if (!sd) {
  668. ret = ERR_PTR(-ENOENT);
  669. goto out_unlock;
  670. }
  671. dentry->d_fsdata = sysfs_get(sd);
  672. /* attach dentry and inode */
  673. inode = sysfs_get_inode(dir->i_sb, sd);
  674. if (!inode) {
  675. ret = ERR_PTR(-ENOMEM);
  676. goto out_unlock;
  677. }
  678. /* instantiate and hash dentry */
  679. ret = d_materialise_unique(dentry, inode);
  680. out_unlock:
  681. mutex_unlock(&sysfs_mutex);
  682. return ret;
  683. }
  684. const struct inode_operations sysfs_dir_inode_operations = {
  685. .lookup = sysfs_lookup,
  686. .permission = sysfs_permission,
  687. .setattr = sysfs_setattr,
  688. .getattr = sysfs_getattr,
  689. .setxattr = sysfs_setxattr,
  690. };
  691. static struct sysfs_dirent *sysfs_leftmost_descendant(struct sysfs_dirent *pos)
  692. {
  693. struct sysfs_dirent *last;
  694. while (true) {
  695. struct rb_node *rbn;
  696. last = pos;
  697. if (sysfs_type(pos) != SYSFS_DIR)
  698. break;
  699. rbn = rb_first(&pos->s_dir.children);
  700. if (!rbn)
  701. break;
  702. pos = to_sysfs_dirent(rbn);
  703. }
  704. return last;
  705. }
  706. /**
  707. * sysfs_next_descendant_post - find the next descendant for post-order walk
  708. * @pos: the current position (%NULL to initiate traversal)
  709. * @root: sysfs_dirent whose descendants to walk
  710. *
  711. * Find the next descendant to visit for post-order traversal of @root's
  712. * descendants. @root is included in the iteration and the last node to be
  713. * visited.
  714. */
  715. static struct sysfs_dirent *sysfs_next_descendant_post(struct sysfs_dirent *pos,
  716. struct sysfs_dirent *root)
  717. {
  718. struct rb_node *rbn;
  719. lockdep_assert_held(&sysfs_mutex);
  720. /* if first iteration, visit leftmost descendant which may be root */
  721. if (!pos)
  722. return sysfs_leftmost_descendant(root);
  723. /* if we visited @root, we're done */
  724. if (pos == root)
  725. return NULL;
  726. /* if there's an unvisited sibling, visit its leftmost descendant */
  727. rbn = rb_next(&pos->s_rb);
  728. if (rbn)
  729. return sysfs_leftmost_descendant(to_sysfs_dirent(rbn));
  730. /* no sibling left, visit parent */
  731. return pos->s_parent;
  732. }
  733. static void __sysfs_remove(struct sysfs_addrm_cxt *acxt,
  734. struct sysfs_dirent *sd)
  735. {
  736. struct sysfs_dirent *pos, *next;
  737. if (!sd)
  738. return;
  739. pr_debug("sysfs %s: removing\n", sd->s_name);
  740. next = NULL;
  741. do {
  742. pos = next;
  743. next = sysfs_next_descendant_post(pos, sd);
  744. if (pos)
  745. sysfs_remove_one(acxt, pos);
  746. } while (next);
  747. }
  748. /**
  749. * sysfs_remove - remove a sysfs_dirent recursively
  750. * @sd: the sysfs_dirent to remove
  751. *
  752. * Remove @sd along with all its subdirectories and files.
  753. */
  754. void sysfs_remove(struct sysfs_dirent *sd)
  755. {
  756. struct sysfs_addrm_cxt acxt;
  757. sysfs_addrm_start(&acxt);
  758. __sysfs_remove(&acxt, sd);
  759. sysfs_addrm_finish(&acxt);
  760. }
  761. /**
  762. * sysfs_hash_and_remove - find a sysfs_dirent by name and remove it
  763. * @dir_sd: parent of the target
  764. * @name: name of the sysfs_dirent to remove
  765. * @ns: namespace tag of the sysfs_dirent to remove
  766. *
  767. * Look for the sysfs_dirent with @name and @ns under @dir_sd and remove
  768. * it. Returns 0 on success, -ENOENT if such entry doesn't exist.
  769. */
  770. int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const char *name,
  771. const void *ns)
  772. {
  773. struct sysfs_addrm_cxt acxt;
  774. struct sysfs_dirent *sd;
  775. if (!dir_sd) {
  776. WARN(1, KERN_WARNING "sysfs: can not remove '%s', no directory\n",
  777. name);
  778. return -ENOENT;
  779. }
  780. sysfs_addrm_start(&acxt);
  781. sd = sysfs_find_dirent(dir_sd, name, ns);
  782. if (sd)
  783. __sysfs_remove(&acxt, sd);
  784. sysfs_addrm_finish(&acxt);
  785. if (sd)
  786. return 0;
  787. else
  788. return -ENOENT;
  789. }
  790. /**
  791. * sysfs_remove_dir - remove an object's directory.
  792. * @kobj: object.
  793. *
  794. * The only thing special about this is that we remove any files in
  795. * the directory before we remove the directory, and we've inlined
  796. * what used to be sysfs_rmdir() below, instead of calling separately.
  797. */
  798. void sysfs_remove_dir(struct kobject *kobj)
  799. {
  800. struct sysfs_dirent *sd = kobj->sd;
  801. /*
  802. * In general, kboject owner is responsible for ensuring removal
  803. * doesn't race with other operations and sysfs doesn't provide any
  804. * protection; however, when @kobj is used as a symlink target, the
  805. * symlinking entity usually doesn't own @kobj and thus has no
  806. * control over removal. @kobj->sd may be removed anytime and
  807. * symlink code may end up dereferencing an already freed sd.
  808. *
  809. * sysfs_symlink_target_lock synchronizes @kobj->sd disassociation
  810. * against symlink operations so that symlink code can safely
  811. * dereference @kobj->sd.
  812. */
  813. spin_lock(&sysfs_symlink_target_lock);
  814. kobj->sd = NULL;
  815. spin_unlock(&sysfs_symlink_target_lock);
  816. if (sd) {
  817. WARN_ON_ONCE(sysfs_type(sd) != SYSFS_DIR);
  818. sysfs_remove(sd);
  819. }
  820. }
  821. int sysfs_rename(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent_sd,
  822. const char *new_name, const void *new_ns)
  823. {
  824. int error;
  825. mutex_lock(&sysfs_mutex);
  826. error = 0;
  827. if ((sd->s_parent == new_parent_sd) && (sd->s_ns == new_ns) &&
  828. (strcmp(sd->s_name, new_name) == 0))
  829. goto out; /* nothing to rename */
  830. error = -EEXIST;
  831. if (sysfs_find_dirent(new_parent_sd, new_name, new_ns))
  832. goto out;
  833. /* rename sysfs_dirent */
  834. if (strcmp(sd->s_name, new_name) != 0) {
  835. error = -ENOMEM;
  836. new_name = kstrdup(new_name, GFP_KERNEL);
  837. if (!new_name)
  838. goto out;
  839. kfree(sd->s_name);
  840. sd->s_name = new_name;
  841. }
  842. /*
  843. * Move to the appropriate place in the appropriate directories rbtree.
  844. */
  845. sysfs_unlink_sibling(sd);
  846. sysfs_get(new_parent_sd);
  847. sysfs_put(sd->s_parent);
  848. sd->s_ns = new_ns;
  849. sd->s_hash = sysfs_name_hash(sd->s_name, sd->s_ns);
  850. sd->s_parent = new_parent_sd;
  851. sysfs_link_sibling(sd);
  852. error = 0;
  853. out:
  854. mutex_unlock(&sysfs_mutex);
  855. return error;
  856. }
  857. int sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
  858. const void *new_ns)
  859. {
  860. struct sysfs_dirent *parent_sd = kobj->sd->s_parent;
  861. return sysfs_rename(kobj->sd, parent_sd, new_name, new_ns);
  862. }
  863. int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj,
  864. const void *new_ns)
  865. {
  866. struct sysfs_dirent *sd = kobj->sd;
  867. struct sysfs_dirent *new_parent_sd;
  868. BUG_ON(!sd->s_parent);
  869. new_parent_sd = new_parent_kobj && new_parent_kobj->sd ?
  870. new_parent_kobj->sd : &sysfs_root;
  871. return sysfs_rename(sd, new_parent_sd, sd->s_name, new_ns);
  872. }
  873. /* Relationship between s_mode and the DT_xxx types */
  874. static inline unsigned char dt_type(struct sysfs_dirent *sd)
  875. {
  876. return (sd->s_mode >> 12) & 15;
  877. }
  878. static int sysfs_dir_release(struct inode *inode, struct file *filp)
  879. {
  880. sysfs_put(filp->private_data);
  881. return 0;
  882. }
  883. static struct sysfs_dirent *sysfs_dir_pos(const void *ns,
  884. struct sysfs_dirent *parent_sd, loff_t hash, struct sysfs_dirent *pos)
  885. {
  886. if (pos) {
  887. int valid = !(pos->s_flags & SYSFS_FLAG_REMOVED) &&
  888. pos->s_parent == parent_sd &&
  889. hash == pos->s_hash;
  890. sysfs_put(pos);
  891. if (!valid)
  892. pos = NULL;
  893. }
  894. if (!pos && (hash > 1) && (hash < INT_MAX)) {
  895. struct rb_node *node = parent_sd->s_dir.children.rb_node;
  896. while (node) {
  897. pos = to_sysfs_dirent(node);
  898. if (hash < pos->s_hash)
  899. node = node->rb_left;
  900. else if (hash > pos->s_hash)
  901. node = node->rb_right;
  902. else
  903. break;
  904. }
  905. }
  906. /* Skip over entries in the wrong namespace */
  907. while (pos && pos->s_ns != ns) {
  908. struct rb_node *node = rb_next(&pos->s_rb);
  909. if (!node)
  910. pos = NULL;
  911. else
  912. pos = to_sysfs_dirent(node);
  913. }
  914. return pos;
  915. }
  916. static struct sysfs_dirent *sysfs_dir_next_pos(const void *ns,
  917. struct sysfs_dirent *parent_sd, ino_t ino, struct sysfs_dirent *pos)
  918. {
  919. pos = sysfs_dir_pos(ns, parent_sd, ino, pos);
  920. if (pos)
  921. do {
  922. struct rb_node *node = rb_next(&pos->s_rb);
  923. if (!node)
  924. pos = NULL;
  925. else
  926. pos = to_sysfs_dirent(node);
  927. } while (pos && pos->s_ns != ns);
  928. return pos;
  929. }
  930. static int sysfs_readdir(struct file *file, struct dir_context *ctx)
  931. {
  932. struct dentry *dentry = file->f_path.dentry;
  933. struct sysfs_dirent *parent_sd = dentry->d_fsdata;
  934. struct sysfs_dirent *pos = file->private_data;
  935. enum kobj_ns_type type;
  936. const void *ns;
  937. type = sysfs_ns_type(parent_sd);
  938. ns = sysfs_info(dentry->d_sb)->ns[type];
  939. if (!dir_emit_dots(file, ctx))
  940. return 0;
  941. mutex_lock(&sysfs_mutex);
  942. for (pos = sysfs_dir_pos(ns, parent_sd, ctx->pos, pos);
  943. pos;
  944. pos = sysfs_dir_next_pos(ns, parent_sd, ctx->pos, pos)) {
  945. const char *name = pos->s_name;
  946. unsigned int type = dt_type(pos);
  947. int len = strlen(name);
  948. ino_t ino = pos->s_ino;
  949. ctx->pos = pos->s_hash;
  950. file->private_data = sysfs_get(pos);
  951. mutex_unlock(&sysfs_mutex);
  952. if (!dir_emit(ctx, name, len, ino, type))
  953. return 0;
  954. mutex_lock(&sysfs_mutex);
  955. }
  956. mutex_unlock(&sysfs_mutex);
  957. file->private_data = NULL;
  958. ctx->pos = INT_MAX;
  959. return 0;
  960. }
  961. static loff_t sysfs_dir_llseek(struct file *file, loff_t offset, int whence)
  962. {
  963. struct inode *inode = file_inode(file);
  964. loff_t ret;
  965. mutex_lock(&inode->i_mutex);
  966. ret = generic_file_llseek(file, offset, whence);
  967. mutex_unlock(&inode->i_mutex);
  968. return ret;
  969. }
  970. const struct file_operations sysfs_dir_operations = {
  971. .read = generic_read_dir,
  972. .iterate = sysfs_readdir,
  973. .release = sysfs_dir_release,
  974. .llseek = sysfs_dir_llseek,
  975. };