dir.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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 void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
  262. {
  263. struct sysfs_dirent * sd = dentry->d_fsdata;
  264. sysfs_put(sd);
  265. iput(inode);
  266. }
  267. static const struct dentry_operations sysfs_dentry_ops = {
  268. .d_iput = sysfs_d_iput,
  269. };
  270. struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
  271. {
  272. char *dup_name = NULL;
  273. struct sysfs_dirent *sd;
  274. if (type & SYSFS_COPY_NAME) {
  275. name = dup_name = kstrdup(name, GFP_KERNEL);
  276. if (!name)
  277. return NULL;
  278. }
  279. sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
  280. if (!sd)
  281. goto err_out1;
  282. if (sysfs_alloc_ino(&sd->s_ino))
  283. goto err_out2;
  284. atomic_set(&sd->s_count, 1);
  285. atomic_set(&sd->s_active, 0);
  286. sd->s_name = name;
  287. sd->s_mode = mode;
  288. sd->s_flags = type;
  289. return sd;
  290. err_out2:
  291. kmem_cache_free(sysfs_dir_cachep, sd);
  292. err_out1:
  293. kfree(dup_name);
  294. return NULL;
  295. }
  296. static int sysfs_ilookup_test(struct inode *inode, void *arg)
  297. {
  298. struct sysfs_dirent *sd = arg;
  299. return inode->i_ino == sd->s_ino;
  300. }
  301. /**
  302. * sysfs_addrm_start - prepare for sysfs_dirent add/remove
  303. * @acxt: pointer to sysfs_addrm_cxt to be used
  304. * @parent_sd: parent sysfs_dirent
  305. *
  306. * This function is called when the caller is about to add or
  307. * remove sysfs_dirent under @parent_sd. This function acquires
  308. * sysfs_mutex, grabs inode for @parent_sd if available and lock
  309. * i_mutex of it. @acxt is used to keep and pass context to
  310. * other addrm functions.
  311. *
  312. * LOCKING:
  313. * Kernel thread context (may sleep). sysfs_mutex is locked on
  314. * return. i_mutex of parent inode is locked on return if
  315. * available.
  316. */
  317. void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt,
  318. struct sysfs_dirent *parent_sd)
  319. {
  320. struct inode *inode;
  321. memset(acxt, 0, sizeof(*acxt));
  322. acxt->parent_sd = parent_sd;
  323. /* Lookup parent inode. inode initialization is protected by
  324. * sysfs_mutex, so inode existence can be determined by
  325. * looking up inode while holding sysfs_mutex.
  326. */
  327. mutex_lock(&sysfs_mutex);
  328. inode = ilookup5(sysfs_sb, parent_sd->s_ino, sysfs_ilookup_test,
  329. parent_sd);
  330. if (inode) {
  331. WARN_ON(inode->i_state & I_NEW);
  332. /* parent inode available */
  333. acxt->parent_inode = inode;
  334. /* sysfs_mutex is below i_mutex in lock hierarchy.
  335. * First, trylock i_mutex. If fails, unlock
  336. * sysfs_mutex and lock them in order.
  337. */
  338. if (!mutex_trylock(&inode->i_mutex)) {
  339. mutex_unlock(&sysfs_mutex);
  340. mutex_lock(&inode->i_mutex);
  341. mutex_lock(&sysfs_mutex);
  342. }
  343. }
  344. }
  345. /**
  346. * __sysfs_add_one - add sysfs_dirent to parent without warning
  347. * @acxt: addrm context to use
  348. * @sd: sysfs_dirent to be added
  349. *
  350. * Get @acxt->parent_sd and set sd->s_parent to it and increment
  351. * nlink of parent inode if @sd is a directory and link into the
  352. * children list of the parent.
  353. *
  354. * This function should be called between calls to
  355. * sysfs_addrm_start() and sysfs_addrm_finish() and should be
  356. * passed the same @acxt as passed to sysfs_addrm_start().
  357. *
  358. * LOCKING:
  359. * Determined by sysfs_addrm_start().
  360. *
  361. * RETURNS:
  362. * 0 on success, -EEXIST if entry with the given name already
  363. * exists.
  364. */
  365. int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
  366. {
  367. if (sysfs_find_dirent(acxt->parent_sd, sd->s_name))
  368. return -EEXIST;
  369. sd->s_parent = sysfs_get(acxt->parent_sd);
  370. if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
  371. inc_nlink(acxt->parent_inode);
  372. acxt->cnt++;
  373. sysfs_link_sibling(sd);
  374. return 0;
  375. }
  376. /**
  377. * sysfs_pathname - return full path to sysfs dirent
  378. * @sd: sysfs_dirent whose path we want
  379. * @path: caller allocated buffer
  380. *
  381. * Gives the name "/" to the sysfs_root entry; any path returned
  382. * is relative to wherever sysfs is mounted.
  383. *
  384. * XXX: does no error checking on @path size
  385. */
  386. static char *sysfs_pathname(struct sysfs_dirent *sd, char *path)
  387. {
  388. if (sd->s_parent) {
  389. sysfs_pathname(sd->s_parent, path);
  390. strcat(path, "/");
  391. }
  392. strcat(path, sd->s_name);
  393. return path;
  394. }
  395. /**
  396. * sysfs_add_one - add sysfs_dirent to parent
  397. * @acxt: addrm context to use
  398. * @sd: sysfs_dirent to be added
  399. *
  400. * Get @acxt->parent_sd and set sd->s_parent to it and increment
  401. * nlink of parent inode if @sd is a directory and link into the
  402. * children list of the parent.
  403. *
  404. * This function should be called between calls to
  405. * sysfs_addrm_start() and sysfs_addrm_finish() and should be
  406. * passed the same @acxt as passed to sysfs_addrm_start().
  407. *
  408. * LOCKING:
  409. * Determined by sysfs_addrm_start().
  410. *
  411. * RETURNS:
  412. * 0 on success, -EEXIST if entry with the given name already
  413. * exists.
  414. */
  415. int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
  416. {
  417. int ret;
  418. ret = __sysfs_add_one(acxt, sd);
  419. if (ret == -EEXIST) {
  420. char *path = kzalloc(PATH_MAX, GFP_KERNEL);
  421. WARN(1, KERN_WARNING
  422. "sysfs: cannot create duplicate filename '%s'\n",
  423. (path == NULL) ? sd->s_name :
  424. strcat(strcat(sysfs_pathname(acxt->parent_sd, path), "/"),
  425. sd->s_name));
  426. kfree(path);
  427. }
  428. return ret;
  429. }
  430. /**
  431. * sysfs_remove_one - remove sysfs_dirent from parent
  432. * @acxt: addrm context to use
  433. * @sd: sysfs_dirent to be removed
  434. *
  435. * Mark @sd removed and drop nlink of parent inode if @sd is a
  436. * directory. @sd is unlinked from the children list.
  437. *
  438. * This function should be called between calls to
  439. * sysfs_addrm_start() and sysfs_addrm_finish() and should be
  440. * passed the same @acxt as passed to sysfs_addrm_start().
  441. *
  442. * LOCKING:
  443. * Determined by sysfs_addrm_start().
  444. */
  445. void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
  446. {
  447. BUG_ON(sd->s_flags & SYSFS_FLAG_REMOVED);
  448. sysfs_unlink_sibling(sd);
  449. sd->s_flags |= SYSFS_FLAG_REMOVED;
  450. sd->s_sibling = acxt->removed;
  451. acxt->removed = sd;
  452. if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
  453. drop_nlink(acxt->parent_inode);
  454. acxt->cnt++;
  455. }
  456. /**
  457. * sysfs_drop_dentry - drop dentry for the specified sysfs_dirent
  458. * @sd: target sysfs_dirent
  459. *
  460. * Drop dentry for @sd. @sd must have been unlinked from its
  461. * parent on entry to this function such that it can't be looked
  462. * up anymore.
  463. */
  464. static void sysfs_drop_dentry(struct sysfs_dirent *sd)
  465. {
  466. struct inode *inode;
  467. struct dentry *dentry;
  468. inode = ilookup(sysfs_sb, sd->s_ino);
  469. if (!inode)
  470. return;
  471. /* Drop any existing dentries associated with sd.
  472. *
  473. * For the dentry to be properly freed we need to grab a
  474. * reference to the dentry under the dcache lock, unhash it,
  475. * and then put it. The playing with the dentry count allows
  476. * dput to immediately free the dentry if it is not in use.
  477. */
  478. repeat:
  479. spin_lock(&dcache_lock);
  480. list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
  481. if (d_unhashed(dentry))
  482. continue;
  483. dget_locked(dentry);
  484. spin_lock(&dentry->d_lock);
  485. __d_drop(dentry);
  486. spin_unlock(&dentry->d_lock);
  487. spin_unlock(&dcache_lock);
  488. dput(dentry);
  489. goto repeat;
  490. }
  491. spin_unlock(&dcache_lock);
  492. /* adjust nlink and update timestamp */
  493. mutex_lock(&inode->i_mutex);
  494. inode->i_ctime = CURRENT_TIME;
  495. drop_nlink(inode);
  496. if (sysfs_type(sd) == SYSFS_DIR)
  497. drop_nlink(inode);
  498. mutex_unlock(&inode->i_mutex);
  499. iput(inode);
  500. }
  501. /**
  502. * sysfs_addrm_finish - finish up sysfs_dirent add/remove
  503. * @acxt: addrm context to finish up
  504. *
  505. * Finish up sysfs_dirent add/remove. Resources acquired by
  506. * sysfs_addrm_start() are released and removed sysfs_dirents are
  507. * cleaned up. Timestamps on the parent inode are updated.
  508. *
  509. * LOCKING:
  510. * All mutexes acquired by sysfs_addrm_start() are released.
  511. */
  512. void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
  513. {
  514. /* release resources acquired by sysfs_addrm_start() */
  515. mutex_unlock(&sysfs_mutex);
  516. if (acxt->parent_inode) {
  517. struct inode *inode = acxt->parent_inode;
  518. /* if added/removed, update timestamps on the parent */
  519. if (acxt->cnt)
  520. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  521. mutex_unlock(&inode->i_mutex);
  522. iput(inode);
  523. }
  524. /* kill removed sysfs_dirents */
  525. while (acxt->removed) {
  526. struct sysfs_dirent *sd = acxt->removed;
  527. acxt->removed = sd->s_sibling;
  528. sd->s_sibling = NULL;
  529. sysfs_drop_dentry(sd);
  530. sysfs_deactivate(sd);
  531. unmap_bin_file(sd);
  532. sysfs_put(sd);
  533. }
  534. }
  535. /**
  536. * sysfs_find_dirent - find sysfs_dirent with the given name
  537. * @parent_sd: sysfs_dirent to search under
  538. * @name: name to look for
  539. *
  540. * Look for sysfs_dirent with name @name under @parent_sd.
  541. *
  542. * LOCKING:
  543. * mutex_lock(sysfs_mutex)
  544. *
  545. * RETURNS:
  546. * Pointer to sysfs_dirent if found, NULL if not.
  547. */
  548. struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
  549. const unsigned char *name)
  550. {
  551. struct sysfs_dirent *sd;
  552. for (sd = parent_sd->s_dir.children; sd; sd = sd->s_sibling)
  553. if (!strcmp(sd->s_name, name))
  554. return sd;
  555. return NULL;
  556. }
  557. /**
  558. * sysfs_get_dirent - find and get sysfs_dirent with the given name
  559. * @parent_sd: sysfs_dirent to search under
  560. * @name: name to look for
  561. *
  562. * Look for sysfs_dirent with name @name under @parent_sd and get
  563. * it if found.
  564. *
  565. * LOCKING:
  566. * Kernel thread context (may sleep). Grabs sysfs_mutex.
  567. *
  568. * RETURNS:
  569. * Pointer to sysfs_dirent if found, NULL if not.
  570. */
  571. struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
  572. const unsigned char *name)
  573. {
  574. struct sysfs_dirent *sd;
  575. mutex_lock(&sysfs_mutex);
  576. sd = sysfs_find_dirent(parent_sd, name);
  577. sysfs_get(sd);
  578. mutex_unlock(&sysfs_mutex);
  579. return sd;
  580. }
  581. EXPORT_SYMBOL_GPL(sysfs_get_dirent);
  582. static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
  583. const char *name, struct sysfs_dirent **p_sd)
  584. {
  585. umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
  586. struct sysfs_addrm_cxt acxt;
  587. struct sysfs_dirent *sd;
  588. int rc;
  589. /* allocate */
  590. sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
  591. if (!sd)
  592. return -ENOMEM;
  593. sd->s_dir.kobj = kobj;
  594. /* link in */
  595. sysfs_addrm_start(&acxt, parent_sd);
  596. rc = sysfs_add_one(&acxt, sd);
  597. sysfs_addrm_finish(&acxt);
  598. if (rc == 0)
  599. *p_sd = sd;
  600. else
  601. sysfs_put(sd);
  602. return rc;
  603. }
  604. int sysfs_create_subdir(struct kobject *kobj, const char *name,
  605. struct sysfs_dirent **p_sd)
  606. {
  607. return create_dir(kobj, kobj->sd, name, p_sd);
  608. }
  609. /**
  610. * sysfs_create_dir - create a directory for an object.
  611. * @kobj: object we're creating directory for.
  612. */
  613. int sysfs_create_dir(struct kobject * kobj)
  614. {
  615. struct sysfs_dirent *parent_sd, *sd;
  616. int error = 0;
  617. BUG_ON(!kobj);
  618. if (kobj->parent)
  619. parent_sd = kobj->parent->sd;
  620. else
  621. parent_sd = &sysfs_root;
  622. error = create_dir(kobj, parent_sd, kobject_name(kobj), &sd);
  623. if (!error)
  624. kobj->sd = sd;
  625. return error;
  626. }
  627. static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
  628. struct nameidata *nd)
  629. {
  630. struct dentry *ret = NULL;
  631. struct sysfs_dirent *parent_sd = dentry->d_parent->d_fsdata;
  632. struct sysfs_dirent *sd;
  633. struct inode *inode;
  634. mutex_lock(&sysfs_mutex);
  635. sd = sysfs_find_dirent(parent_sd, dentry->d_name.name);
  636. /* no such entry */
  637. if (!sd) {
  638. ret = ERR_PTR(-ENOENT);
  639. goto out_unlock;
  640. }
  641. /* attach dentry and inode */
  642. inode = sysfs_get_inode(sd);
  643. if (!inode) {
  644. ret = ERR_PTR(-ENOMEM);
  645. goto out_unlock;
  646. }
  647. /* instantiate and hash dentry */
  648. dentry->d_op = &sysfs_dentry_ops;
  649. dentry->d_fsdata = sysfs_get(sd);
  650. d_instantiate(dentry, inode);
  651. d_rehash(dentry);
  652. out_unlock:
  653. mutex_unlock(&sysfs_mutex);
  654. return ret;
  655. }
  656. const struct inode_operations sysfs_dir_inode_operations = {
  657. .lookup = sysfs_lookup,
  658. .setattr = sysfs_setattr,
  659. .setxattr = sysfs_setxattr,
  660. };
  661. static void remove_dir(struct sysfs_dirent *sd)
  662. {
  663. struct sysfs_addrm_cxt acxt;
  664. sysfs_addrm_start(&acxt, sd->s_parent);
  665. sysfs_remove_one(&acxt, sd);
  666. sysfs_addrm_finish(&acxt);
  667. }
  668. void sysfs_remove_subdir(struct sysfs_dirent *sd)
  669. {
  670. remove_dir(sd);
  671. }
  672. static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
  673. {
  674. struct sysfs_addrm_cxt acxt;
  675. struct sysfs_dirent **pos;
  676. if (!dir_sd)
  677. return;
  678. pr_debug("sysfs %s: removing dir\n", dir_sd->s_name);
  679. sysfs_addrm_start(&acxt, dir_sd);
  680. pos = &dir_sd->s_dir.children;
  681. while (*pos) {
  682. struct sysfs_dirent *sd = *pos;
  683. if (sysfs_type(sd) != SYSFS_DIR)
  684. sysfs_remove_one(&acxt, sd);
  685. else
  686. pos = &(*pos)->s_sibling;
  687. }
  688. sysfs_addrm_finish(&acxt);
  689. remove_dir(dir_sd);
  690. }
  691. /**
  692. * sysfs_remove_dir - remove an object's directory.
  693. * @kobj: object.
  694. *
  695. * The only thing special about this is that we remove any files in
  696. * the directory before we remove the directory, and we've inlined
  697. * what used to be sysfs_rmdir() below, instead of calling separately.
  698. */
  699. void sysfs_remove_dir(struct kobject * kobj)
  700. {
  701. struct sysfs_dirent *sd = kobj->sd;
  702. spin_lock(&sysfs_assoc_lock);
  703. kobj->sd = NULL;
  704. spin_unlock(&sysfs_assoc_lock);
  705. __sysfs_remove_dir(sd);
  706. }
  707. int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
  708. {
  709. struct sysfs_dirent *sd = kobj->sd;
  710. struct dentry *parent = NULL;
  711. struct dentry *old_dentry = NULL, *new_dentry = NULL;
  712. const char *dup_name = NULL;
  713. int error;
  714. mutex_lock(&sysfs_rename_mutex);
  715. error = 0;
  716. if (strcmp(sd->s_name, new_name) == 0)
  717. goto out; /* nothing to rename */
  718. /* get the original dentry */
  719. old_dentry = sysfs_get_dentry(sd);
  720. if (IS_ERR(old_dentry)) {
  721. error = PTR_ERR(old_dentry);
  722. old_dentry = NULL;
  723. goto out;
  724. }
  725. parent = old_dentry->d_parent;
  726. /* lock parent and get dentry for new name */
  727. mutex_lock(&parent->d_inode->i_mutex);
  728. mutex_lock(&sysfs_mutex);
  729. error = -EEXIST;
  730. if (sysfs_find_dirent(sd->s_parent, new_name))
  731. goto out_unlock;
  732. error = -ENOMEM;
  733. new_dentry = d_alloc_name(parent, new_name);
  734. if (!new_dentry)
  735. goto out_unlock;
  736. /* rename sysfs_dirent */
  737. error = -ENOMEM;
  738. new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
  739. if (!new_name)
  740. goto out_unlock;
  741. dup_name = sd->s_name;
  742. sd->s_name = new_name;
  743. /* rename */
  744. d_add(new_dentry, NULL);
  745. d_move(old_dentry, new_dentry);
  746. error = 0;
  747. out_unlock:
  748. mutex_unlock(&sysfs_mutex);
  749. mutex_unlock(&parent->d_inode->i_mutex);
  750. kfree(dup_name);
  751. dput(old_dentry);
  752. dput(new_dentry);
  753. out:
  754. mutex_unlock(&sysfs_rename_mutex);
  755. return error;
  756. }
  757. int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj)
  758. {
  759. struct sysfs_dirent *sd = kobj->sd;
  760. struct sysfs_dirent *new_parent_sd;
  761. struct dentry *old_parent, *new_parent = NULL;
  762. struct dentry *old_dentry = NULL, *new_dentry = NULL;
  763. int error;
  764. mutex_lock(&sysfs_rename_mutex);
  765. BUG_ON(!sd->s_parent);
  766. new_parent_sd = (new_parent_kobj && new_parent_kobj->sd) ?
  767. new_parent_kobj->sd : &sysfs_root;
  768. error = 0;
  769. if (sd->s_parent == new_parent_sd)
  770. goto out; /* nothing to move */
  771. /* get dentries */
  772. old_dentry = sysfs_get_dentry(sd);
  773. if (IS_ERR(old_dentry)) {
  774. error = PTR_ERR(old_dentry);
  775. old_dentry = NULL;
  776. goto out;
  777. }
  778. old_parent = old_dentry->d_parent;
  779. new_parent = sysfs_get_dentry(new_parent_sd);
  780. if (IS_ERR(new_parent)) {
  781. error = PTR_ERR(new_parent);
  782. new_parent = NULL;
  783. goto out;
  784. }
  785. again:
  786. mutex_lock(&old_parent->d_inode->i_mutex);
  787. if (!mutex_trylock(&new_parent->d_inode->i_mutex)) {
  788. mutex_unlock(&old_parent->d_inode->i_mutex);
  789. goto again;
  790. }
  791. mutex_lock(&sysfs_mutex);
  792. error = -EEXIST;
  793. if (sysfs_find_dirent(new_parent_sd, sd->s_name))
  794. goto out_unlock;
  795. error = -ENOMEM;
  796. new_dentry = d_alloc_name(new_parent, sd->s_name);
  797. if (!new_dentry)
  798. goto out_unlock;
  799. error = 0;
  800. d_add(new_dentry, NULL);
  801. d_move(old_dentry, new_dentry);
  802. /* Remove from old parent's list and insert into new parent's list. */
  803. sysfs_unlink_sibling(sd);
  804. sysfs_get(new_parent_sd);
  805. drop_nlink(old_parent->d_inode);
  806. sysfs_put(sd->s_parent);
  807. sd->s_parent = new_parent_sd;
  808. inc_nlink(new_parent->d_inode);
  809. sysfs_link_sibling(sd);
  810. out_unlock:
  811. mutex_unlock(&sysfs_mutex);
  812. mutex_unlock(&new_parent->d_inode->i_mutex);
  813. mutex_unlock(&old_parent->d_inode->i_mutex);
  814. out:
  815. dput(new_parent);
  816. dput(old_dentry);
  817. dput(new_dentry);
  818. mutex_unlock(&sysfs_rename_mutex);
  819. return error;
  820. }
  821. /* Relationship between s_mode and the DT_xxx types */
  822. static inline unsigned char dt_type(struct sysfs_dirent *sd)
  823. {
  824. return (sd->s_mode >> 12) & 15;
  825. }
  826. static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
  827. {
  828. struct dentry *dentry = filp->f_path.dentry;
  829. struct sysfs_dirent * parent_sd = dentry->d_fsdata;
  830. struct sysfs_dirent *pos;
  831. ino_t ino;
  832. if (filp->f_pos == 0) {
  833. ino = parent_sd->s_ino;
  834. if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) == 0)
  835. filp->f_pos++;
  836. }
  837. if (filp->f_pos == 1) {
  838. if (parent_sd->s_parent)
  839. ino = parent_sd->s_parent->s_ino;
  840. else
  841. ino = parent_sd->s_ino;
  842. if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) == 0)
  843. filp->f_pos++;
  844. }
  845. if ((filp->f_pos > 1) && (filp->f_pos < INT_MAX)) {
  846. mutex_lock(&sysfs_mutex);
  847. /* Skip the dentries we have already reported */
  848. pos = parent_sd->s_dir.children;
  849. while (pos && (filp->f_pos > pos->s_ino))
  850. pos = pos->s_sibling;
  851. for ( ; pos; pos = pos->s_sibling) {
  852. const char * name;
  853. int len;
  854. name = pos->s_name;
  855. len = strlen(name);
  856. filp->f_pos = ino = pos->s_ino;
  857. if (filldir(dirent, name, len, filp->f_pos, ino,
  858. dt_type(pos)) < 0)
  859. break;
  860. }
  861. if (!pos)
  862. filp->f_pos = INT_MAX;
  863. mutex_unlock(&sysfs_mutex);
  864. }
  865. return 0;
  866. }
  867. const struct file_operations sysfs_dir_operations = {
  868. .read = generic_read_dir,
  869. .readdir = sysfs_readdir,
  870. .llseek = generic_file_llseek,
  871. };