dir.c 24 KB

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