dir.c 20 KB

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