dir.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. /*
  2. * fs/sysfs/dir.c - sysfs core and dir operation implementation
  3. *
  4. * Copyright (c) 2001-3 Patrick Mochel
  5. * Copyright (c) 2007 SUSE Linux Products GmbH
  6. * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
  7. *
  8. * This file is released under the GPLv2.
  9. *
  10. * Please see Documentation/filesystems/sysfs.txt for more information.
  11. */
  12. #undef DEBUG
  13. #include <linux/fs.h>
  14. #include <linux/mount.h>
  15. #include <linux/module.h>
  16. #include <linux/kobject.h>
  17. #include <linux/namei.h>
  18. #include <linux/idr.h>
  19. #include <linux/completion.h>
  20. #include <linux/mutex.h>
  21. #include <linux/slab.h>
  22. #include <linux/security.h>
  23. #include <linux/hash.h>
  24. #include "sysfs.h"
  25. DEFINE_MUTEX(sysfs_mutex);
  26. DEFINE_SPINLOCK(sysfs_assoc_lock);
  27. #define to_sysfs_dirent(X) rb_entry((X), struct sysfs_dirent, s_rb);
  28. static DEFINE_SPINLOCK(sysfs_ino_lock);
  29. static DEFINE_IDA(sysfs_ino_ida);
  30. /**
  31. * sysfs_name_hash
  32. * @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. while (1) {
  148. int v, t;
  149. v = atomic_read(&sd->s_active);
  150. if (unlikely(v < 0))
  151. return NULL;
  152. t = atomic_cmpxchg(&sd->s_active, v, v + 1);
  153. if (likely(t == v))
  154. break;
  155. if (t < 0)
  156. return NULL;
  157. cpu_relax();
  158. }
  159. if (likely(!ignore_lockdep(sd)))
  160. rwsem_acquire_read(&sd->dep_map, 0, 1, _RET_IP_);
  161. return sd;
  162. }
  163. /**
  164. * sysfs_put_active - put an active reference to sysfs_dirent
  165. * @sd: sysfs_dirent to put an active reference to
  166. *
  167. * Put an active reference to @sd. This function is noop if @sd
  168. * is NULL.
  169. */
  170. void sysfs_put_active(struct sysfs_dirent *sd)
  171. {
  172. int v;
  173. if (unlikely(!sd))
  174. return;
  175. if (likely(!ignore_lockdep(sd)))
  176. rwsem_release(&sd->dep_map, 1, _RET_IP_);
  177. v = atomic_dec_return(&sd->s_active);
  178. if (likely(v != SD_DEACTIVATED_BIAS))
  179. return;
  180. /* atomic_dec_return() is a mb(), we'll always see the updated
  181. * sd->u.completion.
  182. */
  183. complete(sd->u.completion);
  184. }
  185. /**
  186. * sysfs_deactivate - deactivate sysfs_dirent
  187. * @sd: sysfs_dirent to deactivate
  188. *
  189. * Deny new active references and drain existing ones.
  190. */
  191. static void sysfs_deactivate(struct sysfs_dirent *sd)
  192. {
  193. DECLARE_COMPLETION_ONSTACK(wait);
  194. int v;
  195. BUG_ON(!(sd->s_flags & SYSFS_FLAG_REMOVED));
  196. if (!(sysfs_type(sd) & SYSFS_ACTIVE_REF))
  197. return;
  198. sd->u.completion = (void *)&wait;
  199. rwsem_acquire(&sd->dep_map, 0, 0, _RET_IP_);
  200. /* atomic_add_return() is a mb(), put_active() will always see
  201. * the updated sd->u.completion.
  202. */
  203. v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
  204. if (v != SD_DEACTIVATED_BIAS) {
  205. lock_contended(&sd->dep_map, _RET_IP_);
  206. wait_for_completion(&wait);
  207. }
  208. lock_acquired(&sd->dep_map, _RET_IP_);
  209. rwsem_release(&sd->dep_map, 1, _RET_IP_);
  210. }
  211. static int sysfs_alloc_ino(unsigned int *pino)
  212. {
  213. int ino, rc;
  214. retry:
  215. spin_lock(&sysfs_ino_lock);
  216. rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
  217. spin_unlock(&sysfs_ino_lock);
  218. if (rc == -EAGAIN) {
  219. if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
  220. goto retry;
  221. rc = -ENOMEM;
  222. }
  223. *pino = ino;
  224. return rc;
  225. }
  226. static void sysfs_free_ino(unsigned int ino)
  227. {
  228. spin_lock(&sysfs_ino_lock);
  229. ida_remove(&sysfs_ino_ida, ino);
  230. spin_unlock(&sysfs_ino_lock);
  231. }
  232. void release_sysfs_dirent(struct sysfs_dirent * sd)
  233. {
  234. struct sysfs_dirent *parent_sd;
  235. repeat:
  236. /* Moving/renaming is always done while holding reference.
  237. * sd->s_parent won't change beneath us.
  238. */
  239. parent_sd = sd->s_parent;
  240. if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
  241. sysfs_put(sd->s_symlink.target_sd);
  242. if (sysfs_type(sd) & SYSFS_COPY_NAME)
  243. kfree(sd->s_name);
  244. if (sd->s_iattr && sd->s_iattr->ia_secdata)
  245. security_release_secctx(sd->s_iattr->ia_secdata,
  246. sd->s_iattr->ia_secdata_len);
  247. kfree(sd->s_iattr);
  248. sysfs_free_ino(sd->s_ino);
  249. kmem_cache_free(sysfs_dir_cachep, sd);
  250. sd = parent_sd;
  251. if (sd && atomic_dec_and_test(&sd->s_count))
  252. goto repeat;
  253. }
  254. static int sysfs_dentry_delete(const struct dentry *dentry)
  255. {
  256. struct sysfs_dirent *sd = dentry->d_fsdata;
  257. return !!(sd->s_flags & SYSFS_FLAG_REMOVED);
  258. }
  259. static int sysfs_dentry_revalidate(struct dentry *dentry, struct nameidata *nd)
  260. {
  261. struct sysfs_dirent *sd;
  262. int is_dir;
  263. if (nd->flags & LOOKUP_RCU)
  264. return -ECHILD;
  265. sd = dentry->d_fsdata;
  266. mutex_lock(&sysfs_mutex);
  267. /* The sysfs dirent has been deleted */
  268. if (sd->s_flags & SYSFS_FLAG_REMOVED)
  269. goto out_bad;
  270. /* The sysfs dirent has been moved? */
  271. if (dentry->d_parent->d_fsdata != sd->s_parent)
  272. goto out_bad;
  273. /* The sysfs dirent has been renamed */
  274. if (strcmp(dentry->d_name.name, sd->s_name) != 0)
  275. goto out_bad;
  276. mutex_unlock(&sysfs_mutex);
  277. out_valid:
  278. return 1;
  279. out_bad:
  280. /* Remove the dentry from the dcache hashes.
  281. * If this is a deleted dentry we use d_drop instead of d_delete
  282. * so sysfs doesn't need to cope with negative dentries.
  283. *
  284. * If this is a dentry that has simply been renamed we
  285. * use d_drop to remove it from the dcache lookup on its
  286. * old parent. If this dentry persists later when a lookup
  287. * is performed at its new name the dentry will be readded
  288. * to the dcache hashes.
  289. */
  290. is_dir = (sysfs_type(sd) == SYSFS_DIR);
  291. mutex_unlock(&sysfs_mutex);
  292. if (is_dir) {
  293. /* If we have submounts we must allow the vfs caches
  294. * to lie about the state of the filesystem to prevent
  295. * leaks and other nasty things.
  296. */
  297. if (have_submounts(dentry))
  298. goto out_valid;
  299. shrink_dcache_parent(dentry);
  300. }
  301. d_drop(dentry);
  302. return 0;
  303. }
  304. static void sysfs_dentry_iput(struct dentry *dentry, struct inode *inode)
  305. {
  306. struct sysfs_dirent * sd = dentry->d_fsdata;
  307. sysfs_put(sd);
  308. iput(inode);
  309. }
  310. static const struct dentry_operations sysfs_dentry_ops = {
  311. .d_revalidate = sysfs_dentry_revalidate,
  312. .d_delete = sysfs_dentry_delete,
  313. .d_iput = sysfs_dentry_iput,
  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;
  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. return 0;
  404. }
  405. /**
  406. * sysfs_pathname - return full path to sysfs dirent
  407. * @sd: sysfs_dirent whose path we want
  408. * @path: caller allocated buffer
  409. *
  410. * Gives the name "/" to the sysfs_root entry; any path returned
  411. * is relative to wherever sysfs is mounted.
  412. *
  413. * XXX: does no error checking on @path size
  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. strcat(path, "/");
  420. }
  421. strcat(path, sd->s_name);
  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. strcat(strcat(sysfs_pathname(acxt->parent_sd, path), "/"),
  454. sd->s_name));
  455. kfree(path);
  456. }
  457. return ret;
  458. }
  459. /**
  460. * sysfs_remove_one - remove sysfs_dirent from parent
  461. * @acxt: addrm context to use
  462. * @sd: sysfs_dirent to be removed
  463. *
  464. * Mark @sd removed and drop nlink of parent inode if @sd is a
  465. * directory. @sd is unlinked from the children list.
  466. *
  467. * This function should be called between calls to
  468. * sysfs_addrm_start() and sysfs_addrm_finish() and should be
  469. * passed the same @acxt as passed to sysfs_addrm_start().
  470. *
  471. * LOCKING:
  472. * Determined by sysfs_addrm_start().
  473. */
  474. void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
  475. {
  476. struct sysfs_inode_attrs *ps_iattr;
  477. BUG_ON(sd->s_flags & SYSFS_FLAG_REMOVED);
  478. sysfs_unlink_sibling(sd);
  479. /* Update timestamps on the parent */
  480. ps_iattr = acxt->parent_sd->s_iattr;
  481. if (ps_iattr) {
  482. struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
  483. ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
  484. }
  485. sd->s_flags |= SYSFS_FLAG_REMOVED;
  486. sd->u.removed_list = acxt->removed;
  487. acxt->removed = sd;
  488. }
  489. /**
  490. * sysfs_addrm_finish - finish up sysfs_dirent add/remove
  491. * @acxt: addrm context to finish up
  492. *
  493. * Finish up sysfs_dirent add/remove. Resources acquired by
  494. * sysfs_addrm_start() are released and removed sysfs_dirents are
  495. * cleaned up.
  496. *
  497. * LOCKING:
  498. * sysfs_mutex is released.
  499. */
  500. void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
  501. {
  502. /* release resources acquired by sysfs_addrm_start() */
  503. mutex_unlock(&sysfs_mutex);
  504. /* kill removed sysfs_dirents */
  505. while (acxt->removed) {
  506. struct sysfs_dirent *sd = acxt->removed;
  507. acxt->removed = sd->u.removed_list;
  508. sysfs_deactivate(sd);
  509. unmap_bin_file(sd);
  510. sysfs_put(sd);
  511. }
  512. }
  513. /**
  514. * sysfs_find_dirent - find sysfs_dirent with the given name
  515. * @parent_sd: sysfs_dirent to search under
  516. * @name: name to look for
  517. *
  518. * Look for sysfs_dirent with name @name under @parent_sd.
  519. *
  520. * LOCKING:
  521. * mutex_lock(sysfs_mutex)
  522. *
  523. * RETURNS:
  524. * Pointer to sysfs_dirent if found, NULL if not.
  525. */
  526. struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
  527. const void *ns,
  528. const unsigned char *name)
  529. {
  530. struct rb_node *node = parent_sd->s_dir.children.rb_node;
  531. unsigned int hash;
  532. if (!!sysfs_ns_type(parent_sd) != !!ns) {
  533. WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n",
  534. sysfs_ns_type(parent_sd)? "required": "invalid",
  535. parent_sd->s_name, name);
  536. return NULL;
  537. }
  538. hash = sysfs_name_hash(ns, name);
  539. while (node) {
  540. struct sysfs_dirent *sd;
  541. int result;
  542. sd = to_sysfs_dirent(node);
  543. result = sysfs_name_compare(hash, ns, name, sd);
  544. if (result < 0)
  545. node = node->rb_left;
  546. else if (result > 0)
  547. node = node->rb_right;
  548. else
  549. return sd;
  550. }
  551. return NULL;
  552. }
  553. /**
  554. * sysfs_get_dirent - find and get sysfs_dirent with the given name
  555. * @parent_sd: sysfs_dirent to search under
  556. * @name: name to look for
  557. *
  558. * Look for sysfs_dirent with name @name under @parent_sd and get
  559. * it if found.
  560. *
  561. * LOCKING:
  562. * Kernel thread context (may sleep). Grabs sysfs_mutex.
  563. *
  564. * RETURNS:
  565. * Pointer to sysfs_dirent if found, NULL if not.
  566. */
  567. struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
  568. const void *ns,
  569. const unsigned char *name)
  570. {
  571. struct sysfs_dirent *sd;
  572. mutex_lock(&sysfs_mutex);
  573. sd = sysfs_find_dirent(parent_sd, ns, name);
  574. sysfs_get(sd);
  575. mutex_unlock(&sysfs_mutex);
  576. return sd;
  577. }
  578. EXPORT_SYMBOL_GPL(sysfs_get_dirent);
  579. static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
  580. enum kobj_ns_type type, const void *ns, const char *name,
  581. struct sysfs_dirent **p_sd)
  582. {
  583. umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
  584. struct sysfs_addrm_cxt acxt;
  585. struct sysfs_dirent *sd;
  586. int rc;
  587. /* allocate */
  588. sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
  589. if (!sd)
  590. return -ENOMEM;
  591. sd->s_flags |= (type << SYSFS_NS_TYPE_SHIFT);
  592. sd->s_ns = ns;
  593. sd->s_dir.kobj = kobj;
  594. /* link in */
  595. sysfs_addrm_start(&acxt, parent_sd);
  596. rc = sysfs_add_one(&acxt, sd);
  597. sysfs_addrm_finish(&acxt);
  598. if (rc == 0)
  599. *p_sd = sd;
  600. else
  601. sysfs_put(sd);
  602. return rc;
  603. }
  604. int sysfs_create_subdir(struct kobject *kobj, const char *name,
  605. struct sysfs_dirent **p_sd)
  606. {
  607. return create_dir(kobj, kobj->sd,
  608. KOBJ_NS_TYPE_NONE, NULL, name, p_sd);
  609. }
  610. /**
  611. * sysfs_read_ns_type: return associated ns_type
  612. * @kobj: the kobject being queried
  613. *
  614. * Each kobject can be tagged with exactly one namespace type
  615. * (i.e. network or user). Return the ns_type associated with
  616. * this object if any
  617. */
  618. static enum kobj_ns_type sysfs_read_ns_type(struct kobject *kobj)
  619. {
  620. const struct kobj_ns_type_operations *ops;
  621. enum kobj_ns_type type;
  622. ops = kobj_child_ns_ops(kobj);
  623. if (!ops)
  624. return KOBJ_NS_TYPE_NONE;
  625. type = ops->type;
  626. BUG_ON(type <= KOBJ_NS_TYPE_NONE);
  627. BUG_ON(type >= KOBJ_NS_TYPES);
  628. BUG_ON(!kobj_ns_type_registered(type));
  629. return type;
  630. }
  631. /**
  632. * sysfs_create_dir - create a directory for an object.
  633. * @kobj: object we're creating directory for.
  634. */
  635. int sysfs_create_dir(struct kobject * kobj)
  636. {
  637. enum kobj_ns_type type;
  638. struct sysfs_dirent *parent_sd, *sd;
  639. const void *ns = NULL;
  640. int error = 0;
  641. BUG_ON(!kobj);
  642. if (kobj->parent)
  643. parent_sd = kobj->parent->sd;
  644. else
  645. parent_sd = &sysfs_root;
  646. if (!parent_sd)
  647. return -ENOENT;
  648. if (sysfs_ns_type(parent_sd))
  649. ns = kobj->ktype->namespace(kobj);
  650. type = sysfs_read_ns_type(kobj);
  651. error = create_dir(kobj, parent_sd, type, ns, kobject_name(kobj), &sd);
  652. if (!error)
  653. kobj->sd = sd;
  654. return error;
  655. }
  656. static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
  657. struct nameidata *nd)
  658. {
  659. struct dentry *ret = NULL;
  660. struct dentry *parent = dentry->d_parent;
  661. struct sysfs_dirent *parent_sd = parent->d_fsdata;
  662. struct sysfs_dirent *sd;
  663. struct inode *inode;
  664. enum kobj_ns_type type;
  665. const void *ns;
  666. mutex_lock(&sysfs_mutex);
  667. type = sysfs_ns_type(parent_sd);
  668. ns = sysfs_info(dir->i_sb)->ns[type];
  669. sd = sysfs_find_dirent(parent_sd, ns, dentry->d_name.name);
  670. /* no such entry */
  671. if (!sd) {
  672. ret = ERR_PTR(-ENOENT);
  673. goto out_unlock;
  674. }
  675. /* attach dentry and inode */
  676. inode = sysfs_get_inode(dir->i_sb, sd);
  677. if (!inode) {
  678. ret = ERR_PTR(-ENOMEM);
  679. goto out_unlock;
  680. }
  681. /* instantiate and hash dentry */
  682. ret = d_find_alias(inode);
  683. if (!ret) {
  684. d_set_d_op(dentry, &sysfs_dentry_ops);
  685. dentry->d_fsdata = sysfs_get(sd);
  686. d_add(dentry, inode);
  687. } else {
  688. d_move(ret, dentry);
  689. iput(inode);
  690. }
  691. out_unlock:
  692. mutex_unlock(&sysfs_mutex);
  693. return ret;
  694. }
  695. const struct inode_operations sysfs_dir_inode_operations = {
  696. .lookup = sysfs_lookup,
  697. .permission = sysfs_permission,
  698. .setattr = sysfs_setattr,
  699. .getattr = sysfs_getattr,
  700. .setxattr = sysfs_setxattr,
  701. };
  702. static void remove_dir(struct sysfs_dirent *sd)
  703. {
  704. struct sysfs_addrm_cxt acxt;
  705. sysfs_addrm_start(&acxt, sd->s_parent);
  706. sysfs_remove_one(&acxt, sd);
  707. sysfs_addrm_finish(&acxt);
  708. }
  709. void sysfs_remove_subdir(struct sysfs_dirent *sd)
  710. {
  711. remove_dir(sd);
  712. }
  713. static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
  714. {
  715. struct sysfs_addrm_cxt acxt;
  716. struct rb_node *pos;
  717. if (!dir_sd)
  718. return;
  719. pr_debug("sysfs %s: removing dir\n", dir_sd->s_name);
  720. sysfs_addrm_start(&acxt, dir_sd);
  721. pos = rb_first(&dir_sd->s_dir.children);
  722. while (pos) {
  723. struct sysfs_dirent *sd = to_sysfs_dirent(pos);
  724. pos = rb_next(pos);
  725. if (sysfs_type(sd) != SYSFS_DIR)
  726. sysfs_remove_one(&acxt, sd);
  727. }
  728. sysfs_addrm_finish(&acxt);
  729. remove_dir(dir_sd);
  730. }
  731. /**
  732. * sysfs_remove_dir - remove an object's directory.
  733. * @kobj: object.
  734. *
  735. * The only thing special about this is that we remove any files in
  736. * the directory before we remove the directory, and we've inlined
  737. * what used to be sysfs_rmdir() below, instead of calling separately.
  738. */
  739. void sysfs_remove_dir(struct kobject * kobj)
  740. {
  741. struct sysfs_dirent *sd = kobj->sd;
  742. spin_lock(&sysfs_assoc_lock);
  743. kobj->sd = NULL;
  744. spin_unlock(&sysfs_assoc_lock);
  745. __sysfs_remove_dir(sd);
  746. }
  747. int sysfs_rename(struct sysfs_dirent *sd,
  748. struct sysfs_dirent *new_parent_sd, const void *new_ns,
  749. const char *new_name)
  750. {
  751. int error;
  752. mutex_lock(&sysfs_mutex);
  753. error = 0;
  754. if ((sd->s_parent == new_parent_sd) && (sd->s_ns == new_ns) &&
  755. (strcmp(sd->s_name, new_name) == 0))
  756. goto out; /* nothing to rename */
  757. error = -EEXIST;
  758. if (sysfs_find_dirent(new_parent_sd, new_ns, new_name))
  759. goto out;
  760. /* rename sysfs_dirent */
  761. if (strcmp(sd->s_name, new_name) != 0) {
  762. error = -ENOMEM;
  763. new_name = kstrdup(new_name, GFP_KERNEL);
  764. if (!new_name)
  765. goto out;
  766. kfree(sd->s_name);
  767. sd->s_name = new_name;
  768. }
  769. /* Move to the appropriate place in the appropriate directories rbtree. */
  770. sysfs_unlink_sibling(sd);
  771. sysfs_get(new_parent_sd);
  772. sysfs_put(sd->s_parent);
  773. sd->s_ns = new_ns;
  774. sd->s_hash = sysfs_name_hash(sd->s_ns, sd->s_name);
  775. sd->s_parent = new_parent_sd;
  776. sysfs_link_sibling(sd);
  777. error = 0;
  778. out:
  779. mutex_unlock(&sysfs_mutex);
  780. return error;
  781. }
  782. int sysfs_rename_dir(struct kobject *kobj, const char *new_name)
  783. {
  784. struct sysfs_dirent *parent_sd = kobj->sd->s_parent;
  785. const void *new_ns = NULL;
  786. if (sysfs_ns_type(parent_sd))
  787. new_ns = kobj->ktype->namespace(kobj);
  788. return sysfs_rename(kobj->sd, parent_sd, new_ns, new_name);
  789. }
  790. int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj)
  791. {
  792. struct sysfs_dirent *sd = kobj->sd;
  793. struct sysfs_dirent *new_parent_sd;
  794. const void *new_ns = NULL;
  795. BUG_ON(!sd->s_parent);
  796. if (sysfs_ns_type(sd->s_parent))
  797. new_ns = kobj->ktype->namespace(kobj);
  798. new_parent_sd = new_parent_kobj && new_parent_kobj->sd ?
  799. new_parent_kobj->sd : &sysfs_root;
  800. return sysfs_rename(sd, new_parent_sd, new_ns, sd->s_name);
  801. }
  802. /* Relationship between s_mode and the DT_xxx types */
  803. static inline unsigned char dt_type(struct sysfs_dirent *sd)
  804. {
  805. return (sd->s_mode >> 12) & 15;
  806. }
  807. static int sysfs_dir_release(struct inode *inode, struct file *filp)
  808. {
  809. sysfs_put(filp->private_data);
  810. return 0;
  811. }
  812. static struct sysfs_dirent *sysfs_dir_pos(const void *ns,
  813. struct sysfs_dirent *parent_sd, loff_t hash, struct sysfs_dirent *pos)
  814. {
  815. if (pos) {
  816. int valid = !(pos->s_flags & SYSFS_FLAG_REMOVED) &&
  817. pos->s_parent == parent_sd &&
  818. hash == pos->s_hash;
  819. sysfs_put(pos);
  820. if (!valid)
  821. pos = NULL;
  822. }
  823. if (!pos && (hash > 1) && (hash < INT_MAX)) {
  824. struct rb_node *node = parent_sd->s_dir.children.rb_node;
  825. while (node) {
  826. pos = to_sysfs_dirent(node);
  827. if (hash < pos->s_hash)
  828. node = node->rb_left;
  829. else if (hash > pos->s_hash)
  830. node = node->rb_right;
  831. else
  832. break;
  833. }
  834. }
  835. /* Skip over entries in the wrong namespace */
  836. while (pos && pos->s_ns != ns) {
  837. struct rb_node *node = rb_next(&pos->s_rb);
  838. if (!node)
  839. pos = NULL;
  840. else
  841. pos = to_sysfs_dirent(node);
  842. }
  843. return pos;
  844. }
  845. static struct sysfs_dirent *sysfs_dir_next_pos(const void *ns,
  846. struct sysfs_dirent *parent_sd, ino_t ino, struct sysfs_dirent *pos)
  847. {
  848. pos = sysfs_dir_pos(ns, parent_sd, ino, pos);
  849. if (pos) do {
  850. struct rb_node *node = rb_next(&pos->s_rb);
  851. if (!node)
  852. pos = NULL;
  853. else
  854. pos = to_sysfs_dirent(node);
  855. } while (pos && pos->s_ns != ns);
  856. return pos;
  857. }
  858. static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
  859. {
  860. struct dentry *dentry = filp->f_path.dentry;
  861. struct sysfs_dirent * parent_sd = dentry->d_fsdata;
  862. struct sysfs_dirent *pos = filp->private_data;
  863. enum kobj_ns_type type;
  864. const void *ns;
  865. ino_t ino;
  866. type = sysfs_ns_type(parent_sd);
  867. ns = sysfs_info(dentry->d_sb)->ns[type];
  868. if (filp->f_pos == 0) {
  869. ino = parent_sd->s_ino;
  870. if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) == 0)
  871. filp->f_pos++;
  872. }
  873. if (filp->f_pos == 1) {
  874. if (parent_sd->s_parent)
  875. ino = parent_sd->s_parent->s_ino;
  876. else
  877. ino = parent_sd->s_ino;
  878. if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) == 0)
  879. filp->f_pos++;
  880. }
  881. mutex_lock(&sysfs_mutex);
  882. for (pos = sysfs_dir_pos(ns, parent_sd, filp->f_pos, pos);
  883. pos;
  884. pos = sysfs_dir_next_pos(ns, parent_sd, filp->f_pos, pos)) {
  885. const char * name;
  886. unsigned int type;
  887. int len, ret;
  888. name = pos->s_name;
  889. len = strlen(name);
  890. ino = pos->s_ino;
  891. type = dt_type(pos);
  892. filp->f_pos = pos->s_hash;
  893. filp->private_data = sysfs_get(pos);
  894. mutex_unlock(&sysfs_mutex);
  895. ret = filldir(dirent, name, len, filp->f_pos, ino, type);
  896. mutex_lock(&sysfs_mutex);
  897. if (ret < 0)
  898. break;
  899. }
  900. mutex_unlock(&sysfs_mutex);
  901. if ((filp->f_pos > 1) && !pos) { /* EOF */
  902. filp->f_pos = INT_MAX;
  903. filp->private_data = NULL;
  904. }
  905. return 0;
  906. }
  907. const struct file_operations sysfs_dir_operations = {
  908. .read = generic_read_dir,
  909. .readdir = sysfs_readdir,
  910. .release = sysfs_dir_release,
  911. .llseek = generic_file_llseek,
  912. };