dir.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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 "sysfs.h"
  24. DEFINE_MUTEX(sysfs_mutex);
  25. DEFINE_SPINLOCK(sysfs_assoc_lock);
  26. static DEFINE_SPINLOCK(sysfs_ino_lock);
  27. static DEFINE_IDA(sysfs_ino_ida);
  28. /**
  29. * sysfs_link_sibling - link sysfs_dirent into sibling list
  30. * @sd: sysfs_dirent of interest
  31. *
  32. * Link @sd into its sibling list which starts from
  33. * sd->s_parent->s_dir.children.
  34. *
  35. * Locking:
  36. * mutex_lock(sysfs_mutex)
  37. */
  38. static void sysfs_link_sibling(struct sysfs_dirent *sd)
  39. {
  40. struct sysfs_dirent *parent_sd = sd->s_parent;
  41. struct sysfs_dirent **pos;
  42. BUG_ON(sd->s_sibling);
  43. /* Store directory entries in order by ino. This allows
  44. * readdir to properly restart without having to add a
  45. * cursor into the s_dir.children list.
  46. */
  47. for (pos = &parent_sd->s_dir.children; *pos; pos = &(*pos)->s_sibling) {
  48. if (sd->s_ino < (*pos)->s_ino)
  49. break;
  50. }
  51. sd->s_sibling = *pos;
  52. *pos = sd;
  53. }
  54. /**
  55. * sysfs_unlink_sibling - unlink sysfs_dirent from sibling list
  56. * @sd: sysfs_dirent of interest
  57. *
  58. * Unlink @sd from its sibling list which starts from
  59. * sd->s_parent->s_dir.children.
  60. *
  61. * Locking:
  62. * mutex_lock(sysfs_mutex)
  63. */
  64. static void sysfs_unlink_sibling(struct sysfs_dirent *sd)
  65. {
  66. struct sysfs_dirent **pos;
  67. for (pos = &sd->s_parent->s_dir.children; *pos;
  68. pos = &(*pos)->s_sibling) {
  69. if (*pos == sd) {
  70. *pos = sd->s_sibling;
  71. sd->s_sibling = NULL;
  72. break;
  73. }
  74. }
  75. }
  76. /**
  77. * sysfs_get_active - get an active reference to sysfs_dirent
  78. * @sd: sysfs_dirent to get an active reference to
  79. *
  80. * Get an active reference of @sd. This function is noop if @sd
  81. * is NULL.
  82. *
  83. * RETURNS:
  84. * Pointer to @sd on success, NULL on failure.
  85. */
  86. static struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
  87. {
  88. if (unlikely(!sd))
  89. return NULL;
  90. while (1) {
  91. int v, t;
  92. v = atomic_read(&sd->s_active);
  93. if (unlikely(v < 0))
  94. return NULL;
  95. t = atomic_cmpxchg(&sd->s_active, v, v + 1);
  96. if (likely(t == v)) {
  97. rwsem_acquire_read(&sd->dep_map, 0, 1, _RET_IP_);
  98. return sd;
  99. }
  100. if (t < 0)
  101. return NULL;
  102. cpu_relax();
  103. }
  104. }
  105. /**
  106. * sysfs_put_active - put an active reference to sysfs_dirent
  107. * @sd: sysfs_dirent to put an active reference to
  108. *
  109. * Put an active reference to @sd. This function is noop if @sd
  110. * is NULL.
  111. */
  112. static void sysfs_put_active(struct sysfs_dirent *sd)
  113. {
  114. struct completion *cmpl;
  115. int v;
  116. if (unlikely(!sd))
  117. return;
  118. rwsem_release(&sd->dep_map, 1, _RET_IP_);
  119. v = atomic_dec_return(&sd->s_active);
  120. if (likely(v != SD_DEACTIVATED_BIAS))
  121. return;
  122. /* atomic_dec_return() is a mb(), we'll always see the updated
  123. * sd->s_sibling.
  124. */
  125. cmpl = (void *)sd->s_sibling;
  126. complete(cmpl);
  127. }
  128. /**
  129. * sysfs_get_active_two - get active references to sysfs_dirent and parent
  130. * @sd: sysfs_dirent of interest
  131. *
  132. * Get active reference to @sd and its parent. Parent's active
  133. * reference is grabbed first. This function is noop if @sd is
  134. * NULL.
  135. *
  136. * RETURNS:
  137. * Pointer to @sd on success, NULL on failure.
  138. */
  139. struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd)
  140. {
  141. if (sd) {
  142. if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent)))
  143. return NULL;
  144. if (unlikely(!sysfs_get_active(sd))) {
  145. sysfs_put_active(sd->s_parent);
  146. return NULL;
  147. }
  148. }
  149. return sd;
  150. }
  151. /**
  152. * sysfs_put_active_two - put active references to sysfs_dirent and parent
  153. * @sd: sysfs_dirent of interest
  154. *
  155. * Put active references to @sd and its parent. This function is
  156. * noop if @sd is NULL.
  157. */
  158. void sysfs_put_active_two(struct sysfs_dirent *sd)
  159. {
  160. if (sd) {
  161. sysfs_put_active(sd);
  162. sysfs_put_active(sd->s_parent);
  163. }
  164. }
  165. /**
  166. * sysfs_deactivate - deactivate sysfs_dirent
  167. * @sd: sysfs_dirent to deactivate
  168. *
  169. * Deny new active references and drain existing ones.
  170. */
  171. static void sysfs_deactivate(struct sysfs_dirent *sd)
  172. {
  173. DECLARE_COMPLETION_ONSTACK(wait);
  174. int v;
  175. BUG_ON(sd->s_sibling || !(sd->s_flags & SYSFS_FLAG_REMOVED));
  176. sd->s_sibling = (void *)&wait;
  177. rwsem_acquire(&sd->dep_map, 0, 0, _RET_IP_);
  178. /* atomic_add_return() is a mb(), put_active() will always see
  179. * the updated sd->s_sibling.
  180. */
  181. v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
  182. if (v != SD_DEACTIVATED_BIAS) {
  183. lock_contended(&sd->dep_map, _RET_IP_);
  184. wait_for_completion(&wait);
  185. }
  186. sd->s_sibling = NULL;
  187. lock_acquired(&sd->dep_map, _RET_IP_);
  188. rwsem_release(&sd->dep_map, 1, _RET_IP_);
  189. }
  190. static int sysfs_alloc_ino(ino_t *pino)
  191. {
  192. int ino, rc;
  193. retry:
  194. spin_lock(&sysfs_ino_lock);
  195. rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
  196. spin_unlock(&sysfs_ino_lock);
  197. if (rc == -EAGAIN) {
  198. if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
  199. goto retry;
  200. rc = -ENOMEM;
  201. }
  202. *pino = ino;
  203. return rc;
  204. }
  205. static void sysfs_free_ino(ino_t ino)
  206. {
  207. spin_lock(&sysfs_ino_lock);
  208. ida_remove(&sysfs_ino_ida, ino);
  209. spin_unlock(&sysfs_ino_lock);
  210. }
  211. void release_sysfs_dirent(struct sysfs_dirent * sd)
  212. {
  213. struct sysfs_dirent *parent_sd;
  214. repeat:
  215. /* Moving/renaming is always done while holding reference.
  216. * sd->s_parent won't change beneath us.
  217. */
  218. parent_sd = sd->s_parent;
  219. if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
  220. sysfs_put(sd->s_symlink.target_sd);
  221. if (sysfs_type(sd) & SYSFS_COPY_NAME)
  222. kfree(sd->s_name);
  223. if (sd->s_iattr && sd->s_iattr->ia_secdata)
  224. security_release_secctx(sd->s_iattr->ia_secdata,
  225. sd->s_iattr->ia_secdata_len);
  226. kfree(sd->s_iattr);
  227. sysfs_free_ino(sd->s_ino);
  228. kmem_cache_free(sysfs_dir_cachep, sd);
  229. sd = parent_sd;
  230. if (sd && atomic_dec_and_test(&sd->s_count))
  231. goto repeat;
  232. }
  233. static int sysfs_dentry_delete(struct dentry *dentry)
  234. {
  235. struct sysfs_dirent *sd = dentry->d_fsdata;
  236. return !!(sd->s_flags & SYSFS_FLAG_REMOVED);
  237. }
  238. static int sysfs_dentry_revalidate(struct dentry *dentry, struct nameidata *nd)
  239. {
  240. struct sysfs_dirent *sd = dentry->d_fsdata;
  241. int is_dir;
  242. mutex_lock(&sysfs_mutex);
  243. /* The sysfs dirent has been deleted */
  244. if (sd->s_flags & SYSFS_FLAG_REMOVED)
  245. goto out_bad;
  246. /* The sysfs dirent has been moved? */
  247. if (dentry->d_parent->d_fsdata != sd->s_parent)
  248. goto out_bad;
  249. /* The sysfs dirent has been renamed */
  250. if (strcmp(dentry->d_name.name, sd->s_name) != 0)
  251. goto out_bad;
  252. mutex_unlock(&sysfs_mutex);
  253. out_valid:
  254. return 1;
  255. out_bad:
  256. /* Remove the dentry from the dcache hashes.
  257. * If this is a deleted dentry we use d_drop instead of d_delete
  258. * so sysfs doesn't need to cope with negative dentries.
  259. *
  260. * If this is a dentry that has simply been renamed we
  261. * use d_drop to remove it from the dcache lookup on its
  262. * old parent. If this dentry persists later when a lookup
  263. * is performed at its new name the dentry will be readded
  264. * to the dcache hashes.
  265. */
  266. is_dir = (sysfs_type(sd) == SYSFS_DIR);
  267. mutex_unlock(&sysfs_mutex);
  268. if (is_dir) {
  269. /* If we have submounts we must allow the vfs caches
  270. * to lie about the state of the filesystem to prevent
  271. * leaks and other nasty things.
  272. */
  273. if (have_submounts(dentry))
  274. goto out_valid;
  275. shrink_dcache_parent(dentry);
  276. }
  277. d_drop(dentry);
  278. return 0;
  279. }
  280. static void sysfs_dentry_iput(struct dentry *dentry, struct inode *inode)
  281. {
  282. struct sysfs_dirent * sd = dentry->d_fsdata;
  283. sysfs_put(sd);
  284. iput(inode);
  285. }
  286. static const struct dentry_operations sysfs_dentry_ops = {
  287. .d_revalidate = sysfs_dentry_revalidate,
  288. .d_delete = sysfs_dentry_delete,
  289. .d_iput = sysfs_dentry_iput,
  290. };
  291. struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
  292. {
  293. char *dup_name = NULL;
  294. struct sysfs_dirent *sd;
  295. if (type & SYSFS_COPY_NAME) {
  296. name = dup_name = kstrdup(name, GFP_KERNEL);
  297. if (!name)
  298. return NULL;
  299. }
  300. sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
  301. if (!sd)
  302. goto err_out1;
  303. if (sysfs_alloc_ino(&sd->s_ino))
  304. goto err_out2;
  305. atomic_set(&sd->s_count, 1);
  306. atomic_set(&sd->s_active, 0);
  307. sysfs_dirent_init_lockdep(sd);
  308. sd->s_name = name;
  309. sd->s_mode = mode;
  310. sd->s_flags = type;
  311. return sd;
  312. err_out2:
  313. kmem_cache_free(sysfs_dir_cachep, sd);
  314. err_out1:
  315. kfree(dup_name);
  316. return NULL;
  317. }
  318. /**
  319. * sysfs_addrm_start - prepare for sysfs_dirent add/remove
  320. * @acxt: pointer to sysfs_addrm_cxt to be used
  321. * @parent_sd: parent sysfs_dirent
  322. *
  323. * This function is called when the caller is about to add or
  324. * remove sysfs_dirent under @parent_sd. This function acquires
  325. * sysfs_mutex. @acxt is used to keep and pass context to
  326. * other addrm functions.
  327. *
  328. * LOCKING:
  329. * Kernel thread context (may sleep). sysfs_mutex is locked on
  330. * return.
  331. */
  332. void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt,
  333. struct sysfs_dirent *parent_sd)
  334. {
  335. memset(acxt, 0, sizeof(*acxt));
  336. acxt->parent_sd = parent_sd;
  337. mutex_lock(&sysfs_mutex);
  338. }
  339. /**
  340. * __sysfs_add_one - add sysfs_dirent to parent without warning
  341. * @acxt: addrm context to use
  342. * @sd: sysfs_dirent to be added
  343. *
  344. * Get @acxt->parent_sd and set sd->s_parent to it and increment
  345. * nlink of parent inode if @sd is a directory and link into the
  346. * children list of the parent.
  347. *
  348. * This function should be called between calls to
  349. * sysfs_addrm_start() and sysfs_addrm_finish() and should be
  350. * passed the same @acxt as passed to sysfs_addrm_start().
  351. *
  352. * LOCKING:
  353. * Determined by sysfs_addrm_start().
  354. *
  355. * RETURNS:
  356. * 0 on success, -EEXIST if entry with the given name already
  357. * exists.
  358. */
  359. int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
  360. {
  361. struct sysfs_inode_attrs *ps_iattr;
  362. if (sysfs_find_dirent(acxt->parent_sd, sd->s_name))
  363. return -EEXIST;
  364. sd->s_parent = sysfs_get(acxt->parent_sd);
  365. sysfs_link_sibling(sd);
  366. /* Update timestamps on the parent */
  367. ps_iattr = acxt->parent_sd->s_iattr;
  368. if (ps_iattr) {
  369. struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
  370. ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
  371. }
  372. return 0;
  373. }
  374. /**
  375. * sysfs_pathname - return full path to sysfs dirent
  376. * @sd: sysfs_dirent whose path we want
  377. * @path: caller allocated buffer
  378. *
  379. * Gives the name "/" to the sysfs_root entry; any path returned
  380. * is relative to wherever sysfs is mounted.
  381. *
  382. * XXX: does no error checking on @path size
  383. */
  384. static char *sysfs_pathname(struct sysfs_dirent *sd, char *path)
  385. {
  386. if (sd->s_parent) {
  387. sysfs_pathname(sd->s_parent, path);
  388. strcat(path, "/");
  389. }
  390. strcat(path, sd->s_name);
  391. return path;
  392. }
  393. /**
  394. * sysfs_add_one - add sysfs_dirent to parent
  395. * @acxt: addrm context to use
  396. * @sd: sysfs_dirent to be added
  397. *
  398. * Get @acxt->parent_sd and set sd->s_parent to it and increment
  399. * nlink of parent inode if @sd is a directory and link into the
  400. * children list of the parent.
  401. *
  402. * This function should be called between calls to
  403. * sysfs_addrm_start() and sysfs_addrm_finish() and should be
  404. * passed the same @acxt as passed to sysfs_addrm_start().
  405. *
  406. * LOCKING:
  407. * Determined by sysfs_addrm_start().
  408. *
  409. * RETURNS:
  410. * 0 on success, -EEXIST if entry with the given name already
  411. * exists.
  412. */
  413. int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
  414. {
  415. int ret;
  416. ret = __sysfs_add_one(acxt, sd);
  417. if (ret == -EEXIST) {
  418. char *path = kzalloc(PATH_MAX, GFP_KERNEL);
  419. WARN(1, KERN_WARNING
  420. "sysfs: cannot create duplicate filename '%s'\n",
  421. (path == NULL) ? sd->s_name :
  422. strcat(strcat(sysfs_pathname(acxt->parent_sd, path), "/"),
  423. sd->s_name));
  424. kfree(path);
  425. }
  426. return ret;
  427. }
  428. /**
  429. * sysfs_remove_one - remove sysfs_dirent from parent
  430. * @acxt: addrm context to use
  431. * @sd: sysfs_dirent to be removed
  432. *
  433. * Mark @sd removed and drop nlink of parent inode if @sd is a
  434. * directory. @sd is unlinked from the children list.
  435. *
  436. * This function should be called between calls to
  437. * sysfs_addrm_start() and sysfs_addrm_finish() and should be
  438. * passed the same @acxt as passed to sysfs_addrm_start().
  439. *
  440. * LOCKING:
  441. * Determined by sysfs_addrm_start().
  442. */
  443. void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
  444. {
  445. struct sysfs_inode_attrs *ps_iattr;
  446. BUG_ON(sd->s_flags & SYSFS_FLAG_REMOVED);
  447. sysfs_unlink_sibling(sd);
  448. /* Update timestamps on the parent */
  449. ps_iattr = acxt->parent_sd->s_iattr;
  450. if (ps_iattr) {
  451. struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
  452. ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
  453. }
  454. sd->s_flags |= SYSFS_FLAG_REMOVED;
  455. sd->s_sibling = acxt->removed;
  456. acxt->removed = sd;
  457. }
  458. /**
  459. * sysfs_addrm_finish - finish up sysfs_dirent add/remove
  460. * @acxt: addrm context to finish up
  461. *
  462. * Finish up sysfs_dirent add/remove. Resources acquired by
  463. * sysfs_addrm_start() are released and removed sysfs_dirents are
  464. * cleaned up.
  465. *
  466. * LOCKING:
  467. * sysfs_mutex is released.
  468. */
  469. void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
  470. {
  471. /* release resources acquired by sysfs_addrm_start() */
  472. mutex_unlock(&sysfs_mutex);
  473. /* kill removed sysfs_dirents */
  474. while (acxt->removed) {
  475. struct sysfs_dirent *sd = acxt->removed;
  476. acxt->removed = sd->s_sibling;
  477. sd->s_sibling = NULL;
  478. sysfs_deactivate(sd);
  479. unmap_bin_file(sd);
  480. sysfs_put(sd);
  481. }
  482. }
  483. /**
  484. * sysfs_find_dirent - find sysfs_dirent with the given name
  485. * @parent_sd: sysfs_dirent to search under
  486. * @name: name to look for
  487. *
  488. * Look for sysfs_dirent with name @name under @parent_sd.
  489. *
  490. * LOCKING:
  491. * mutex_lock(sysfs_mutex)
  492. *
  493. * RETURNS:
  494. * Pointer to sysfs_dirent if found, NULL if not.
  495. */
  496. struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
  497. const unsigned char *name)
  498. {
  499. struct sysfs_dirent *sd;
  500. for (sd = parent_sd->s_dir.children; sd; sd = sd->s_sibling)
  501. if (!strcmp(sd->s_name, name))
  502. return sd;
  503. return NULL;
  504. }
  505. /**
  506. * sysfs_get_dirent - find and get sysfs_dirent with the given name
  507. * @parent_sd: sysfs_dirent to search under
  508. * @name: name to look for
  509. *
  510. * Look for sysfs_dirent with name @name under @parent_sd and get
  511. * it if found.
  512. *
  513. * LOCKING:
  514. * Kernel thread context (may sleep). Grabs sysfs_mutex.
  515. *
  516. * RETURNS:
  517. * Pointer to sysfs_dirent if found, NULL if not.
  518. */
  519. struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
  520. const unsigned char *name)
  521. {
  522. struct sysfs_dirent *sd;
  523. mutex_lock(&sysfs_mutex);
  524. sd = sysfs_find_dirent(parent_sd, name);
  525. sysfs_get(sd);
  526. mutex_unlock(&sysfs_mutex);
  527. return sd;
  528. }
  529. EXPORT_SYMBOL_GPL(sysfs_get_dirent);
  530. static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
  531. const char *name, struct sysfs_dirent **p_sd)
  532. {
  533. umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
  534. struct sysfs_addrm_cxt acxt;
  535. struct sysfs_dirent *sd;
  536. int rc;
  537. /* allocate */
  538. sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
  539. if (!sd)
  540. return -ENOMEM;
  541. sd->s_dir.kobj = kobj;
  542. /* link in */
  543. sysfs_addrm_start(&acxt, parent_sd);
  544. rc = sysfs_add_one(&acxt, sd);
  545. sysfs_addrm_finish(&acxt);
  546. if (rc == 0)
  547. *p_sd = sd;
  548. else
  549. sysfs_put(sd);
  550. return rc;
  551. }
  552. int sysfs_create_subdir(struct kobject *kobj, const char *name,
  553. struct sysfs_dirent **p_sd)
  554. {
  555. return create_dir(kobj, kobj->sd, name, p_sd);
  556. }
  557. /**
  558. * sysfs_create_dir - create a directory for an object.
  559. * @kobj: object we're creating directory for.
  560. */
  561. int sysfs_create_dir(struct kobject * kobj)
  562. {
  563. struct sysfs_dirent *parent_sd, *sd;
  564. int error = 0;
  565. BUG_ON(!kobj);
  566. if (kobj->parent)
  567. parent_sd = kobj->parent->sd;
  568. else
  569. parent_sd = &sysfs_root;
  570. error = create_dir(kobj, parent_sd, kobject_name(kobj), &sd);
  571. if (!error)
  572. kobj->sd = sd;
  573. return error;
  574. }
  575. static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
  576. struct nameidata *nd)
  577. {
  578. struct dentry *ret = NULL;
  579. struct sysfs_dirent *parent_sd = dentry->d_parent->d_fsdata;
  580. struct sysfs_dirent *sd;
  581. struct inode *inode;
  582. mutex_lock(&sysfs_mutex);
  583. sd = sysfs_find_dirent(parent_sd, dentry->d_name.name);
  584. /* no such entry */
  585. if (!sd) {
  586. ret = ERR_PTR(-ENOENT);
  587. goto out_unlock;
  588. }
  589. /* attach dentry and inode */
  590. inode = sysfs_get_inode(sd);
  591. if (!inode) {
  592. ret = ERR_PTR(-ENOMEM);
  593. goto out_unlock;
  594. }
  595. /* instantiate and hash dentry */
  596. ret = d_find_alias(inode);
  597. if (!ret) {
  598. dentry->d_op = &sysfs_dentry_ops;
  599. dentry->d_fsdata = sysfs_get(sd);
  600. d_add(dentry, inode);
  601. } else {
  602. d_move(ret, dentry);
  603. iput(inode);
  604. }
  605. out_unlock:
  606. mutex_unlock(&sysfs_mutex);
  607. return ret;
  608. }
  609. const struct inode_operations sysfs_dir_inode_operations = {
  610. .lookup = sysfs_lookup,
  611. .permission = sysfs_permission,
  612. .setattr = sysfs_setattr,
  613. .getattr = sysfs_getattr,
  614. .setxattr = sysfs_setxattr,
  615. };
  616. static void remove_dir(struct sysfs_dirent *sd)
  617. {
  618. struct sysfs_addrm_cxt acxt;
  619. sysfs_addrm_start(&acxt, sd->s_parent);
  620. sysfs_remove_one(&acxt, sd);
  621. sysfs_addrm_finish(&acxt);
  622. }
  623. void sysfs_remove_subdir(struct sysfs_dirent *sd)
  624. {
  625. remove_dir(sd);
  626. }
  627. static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
  628. {
  629. struct sysfs_addrm_cxt acxt;
  630. struct sysfs_dirent **pos;
  631. if (!dir_sd)
  632. return;
  633. pr_debug("sysfs %s: removing dir\n", dir_sd->s_name);
  634. sysfs_addrm_start(&acxt, dir_sd);
  635. pos = &dir_sd->s_dir.children;
  636. while (*pos) {
  637. struct sysfs_dirent *sd = *pos;
  638. if (sysfs_type(sd) != SYSFS_DIR)
  639. sysfs_remove_one(&acxt, sd);
  640. else
  641. pos = &(*pos)->s_sibling;
  642. }
  643. sysfs_addrm_finish(&acxt);
  644. remove_dir(dir_sd);
  645. }
  646. /**
  647. * sysfs_remove_dir - remove an object's directory.
  648. * @kobj: object.
  649. *
  650. * The only thing special about this is that we remove any files in
  651. * the directory before we remove the directory, and we've inlined
  652. * what used to be sysfs_rmdir() below, instead of calling separately.
  653. */
  654. void sysfs_remove_dir(struct kobject * kobj)
  655. {
  656. struct sysfs_dirent *sd = kobj->sd;
  657. spin_lock(&sysfs_assoc_lock);
  658. kobj->sd = NULL;
  659. spin_unlock(&sysfs_assoc_lock);
  660. __sysfs_remove_dir(sd);
  661. }
  662. int sysfs_rename(struct sysfs_dirent *sd,
  663. struct sysfs_dirent *new_parent_sd, const char *new_name)
  664. {
  665. const char *dup_name = NULL;
  666. int error;
  667. mutex_lock(&sysfs_mutex);
  668. error = 0;
  669. if ((sd->s_parent == new_parent_sd) &&
  670. (strcmp(sd->s_name, new_name) == 0))
  671. goto out; /* nothing to rename */
  672. error = -EEXIST;
  673. if (sysfs_find_dirent(new_parent_sd, new_name))
  674. goto out;
  675. /* rename sysfs_dirent */
  676. if (strcmp(sd->s_name, new_name) != 0) {
  677. error = -ENOMEM;
  678. new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
  679. if (!new_name)
  680. goto out;
  681. dup_name = sd->s_name;
  682. sd->s_name = new_name;
  683. }
  684. /* Remove from old parent's list and insert into new parent's list. */
  685. if (sd->s_parent != new_parent_sd) {
  686. sysfs_unlink_sibling(sd);
  687. sysfs_get(new_parent_sd);
  688. sysfs_put(sd->s_parent);
  689. sd->s_parent = new_parent_sd;
  690. sysfs_link_sibling(sd);
  691. }
  692. error = 0;
  693. out:
  694. mutex_unlock(&sysfs_mutex);
  695. kfree(dup_name);
  696. return error;
  697. }
  698. int sysfs_rename_dir(struct kobject *kobj, const char *new_name)
  699. {
  700. return sysfs_rename(kobj->sd, kobj->sd->s_parent, new_name);
  701. }
  702. int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj)
  703. {
  704. struct sysfs_dirent *sd = kobj->sd;
  705. struct sysfs_dirent *new_parent_sd;
  706. BUG_ON(!sd->s_parent);
  707. new_parent_sd = new_parent_kobj && new_parent_kobj->sd ?
  708. new_parent_kobj->sd : &sysfs_root;
  709. return sysfs_rename(sd, new_parent_sd, sd->s_name);
  710. }
  711. /* Relationship between s_mode and the DT_xxx types */
  712. static inline unsigned char dt_type(struct sysfs_dirent *sd)
  713. {
  714. return (sd->s_mode >> 12) & 15;
  715. }
  716. static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
  717. {
  718. struct dentry *dentry = filp->f_path.dentry;
  719. struct sysfs_dirent * parent_sd = dentry->d_fsdata;
  720. struct sysfs_dirent *pos;
  721. ino_t ino;
  722. if (filp->f_pos == 0) {
  723. ino = parent_sd->s_ino;
  724. if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) == 0)
  725. filp->f_pos++;
  726. }
  727. if (filp->f_pos == 1) {
  728. if (parent_sd->s_parent)
  729. ino = parent_sd->s_parent->s_ino;
  730. else
  731. ino = parent_sd->s_ino;
  732. if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) == 0)
  733. filp->f_pos++;
  734. }
  735. if ((filp->f_pos > 1) && (filp->f_pos < INT_MAX)) {
  736. mutex_lock(&sysfs_mutex);
  737. /* Skip the dentries we have already reported */
  738. pos = parent_sd->s_dir.children;
  739. while (pos && (filp->f_pos > pos->s_ino))
  740. pos = pos->s_sibling;
  741. for ( ; pos; pos = pos->s_sibling) {
  742. const char * name;
  743. int len;
  744. name = pos->s_name;
  745. len = strlen(name);
  746. filp->f_pos = ino = pos->s_ino;
  747. if (filldir(dirent, name, len, filp->f_pos, ino,
  748. dt_type(pos)) < 0)
  749. break;
  750. }
  751. if (!pos)
  752. filp->f_pos = INT_MAX;
  753. mutex_unlock(&sysfs_mutex);
  754. }
  755. return 0;
  756. }
  757. const struct file_operations sysfs_dir_operations = {
  758. .read = generic_read_dir,
  759. .readdir = sysfs_readdir,
  760. .llseek = generic_file_llseek,
  761. };