dir.c 25 KB

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