dir.c 20 KB

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