dir.c 22 KB

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