dir.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. /*
  2. * dir.c - Operations for sysfs directories.
  3. */
  4. #undef DEBUG
  5. #include <linux/fs.h>
  6. #include <linux/mount.h>
  7. #include <linux/module.h>
  8. #include <linux/kobject.h>
  9. #include <linux/namei.h>
  10. #include <linux/idr.h>
  11. #include <linux/completion.h>
  12. #include <linux/mutex.h>
  13. #include "sysfs.h"
  14. DEFINE_MUTEX(sysfs_mutex);
  15. spinlock_t sysfs_assoc_lock = SPIN_LOCK_UNLOCKED;
  16. static spinlock_t sysfs_ino_lock = SPIN_LOCK_UNLOCKED;
  17. static DEFINE_IDA(sysfs_ino_ida);
  18. /**
  19. * sysfs_link_sibling - link sysfs_dirent into sibling list
  20. * @sd: sysfs_dirent of interest
  21. *
  22. * Link @sd into its sibling list which starts from
  23. * sd->s_parent->s_children.
  24. *
  25. * Locking:
  26. * mutex_lock(sysfs_mutex)
  27. */
  28. static void sysfs_link_sibling(struct sysfs_dirent *sd)
  29. {
  30. struct sysfs_dirent *parent_sd = sd->s_parent;
  31. BUG_ON(sd->s_sibling);
  32. sd->s_sibling = parent_sd->s_children;
  33. parent_sd->s_children = sd;
  34. }
  35. /**
  36. * sysfs_unlink_sibling - unlink sysfs_dirent from sibling list
  37. * @sd: sysfs_dirent of interest
  38. *
  39. * Unlink @sd from its sibling list which starts from
  40. * sd->s_parent->s_children.
  41. *
  42. * Locking:
  43. * mutex_lock(sysfs_mutex)
  44. */
  45. static void sysfs_unlink_sibling(struct sysfs_dirent *sd)
  46. {
  47. struct sysfs_dirent **pos;
  48. for (pos = &sd->s_parent->s_children; *pos; pos = &(*pos)->s_sibling) {
  49. if (*pos == sd) {
  50. *pos = sd->s_sibling;
  51. sd->s_sibling = NULL;
  52. break;
  53. }
  54. }
  55. }
  56. /**
  57. * sysfs_get_dentry - get dentry for the given sysfs_dirent
  58. * @sd: sysfs_dirent of interest
  59. *
  60. * Get dentry for @sd. Dentry is looked up if currently not
  61. * present. This function climbs sysfs_dirent tree till it
  62. * reaches a sysfs_dirent with valid dentry attached and descends
  63. * down from there looking up dentry for each step.
  64. *
  65. * LOCKING:
  66. * Kernel thread context (may sleep)
  67. *
  68. * RETURNS:
  69. * Pointer to found dentry on success, ERR_PTR() value on error.
  70. */
  71. struct dentry *sysfs_get_dentry(struct sysfs_dirent *sd)
  72. {
  73. struct sysfs_dirent *cur;
  74. struct dentry *parent_dentry, *dentry;
  75. int i, depth;
  76. /* Find the first parent which has valid s_dentry and get the
  77. * dentry.
  78. */
  79. mutex_lock(&sysfs_mutex);
  80. restart0:
  81. spin_lock(&sysfs_assoc_lock);
  82. restart1:
  83. spin_lock(&dcache_lock);
  84. dentry = NULL;
  85. depth = 0;
  86. cur = sd;
  87. while (!cur->s_dentry || !cur->s_dentry->d_inode) {
  88. if (cur->s_flags & SYSFS_FLAG_REMOVED) {
  89. dentry = ERR_PTR(-ENOENT);
  90. depth = 0;
  91. break;
  92. }
  93. cur = cur->s_parent;
  94. depth++;
  95. }
  96. if (!IS_ERR(dentry))
  97. dentry = dget_locked(cur->s_dentry);
  98. spin_unlock(&dcache_lock);
  99. spin_unlock(&sysfs_assoc_lock);
  100. /* from the found dentry, look up depth times */
  101. while (depth--) {
  102. /* find and get depth'th ancestor */
  103. for (cur = sd, i = 0; cur && i < depth; i++)
  104. cur = cur->s_parent;
  105. /* This can happen if tree structure was modified due
  106. * to move/rename. Restart.
  107. */
  108. if (i != depth) {
  109. dput(dentry);
  110. goto restart0;
  111. }
  112. sysfs_get(cur);
  113. mutex_unlock(&sysfs_mutex);
  114. /* look it up */
  115. parent_dentry = dentry;
  116. mutex_lock(&parent_dentry->d_inode->i_mutex);
  117. dentry = lookup_one_len_kern(cur->s_name, parent_dentry,
  118. strlen(cur->s_name));
  119. mutex_unlock(&parent_dentry->d_inode->i_mutex);
  120. dput(parent_dentry);
  121. if (IS_ERR(dentry)) {
  122. sysfs_put(cur);
  123. return dentry;
  124. }
  125. mutex_lock(&sysfs_mutex);
  126. spin_lock(&sysfs_assoc_lock);
  127. /* This, again, can happen if tree structure has
  128. * changed and we looked up the wrong thing. Restart.
  129. */
  130. if (cur->s_dentry != dentry) {
  131. dput(dentry);
  132. sysfs_put(cur);
  133. goto restart1;
  134. }
  135. spin_unlock(&sysfs_assoc_lock);
  136. sysfs_put(cur);
  137. }
  138. mutex_unlock(&sysfs_mutex);
  139. return dentry;
  140. }
  141. /**
  142. * sysfs_get_active - get an active reference to sysfs_dirent
  143. * @sd: sysfs_dirent to get an active reference to
  144. *
  145. * Get an active reference of @sd. This function is noop if @sd
  146. * is NULL.
  147. *
  148. * RETURNS:
  149. * Pointer to @sd on success, NULL on failure.
  150. */
  151. struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
  152. {
  153. if (unlikely(!sd))
  154. return NULL;
  155. while (1) {
  156. int v, t;
  157. v = atomic_read(&sd->s_active);
  158. if (unlikely(v < 0))
  159. return NULL;
  160. t = atomic_cmpxchg(&sd->s_active, v, v + 1);
  161. if (likely(t == v))
  162. return sd;
  163. if (t < 0)
  164. return NULL;
  165. cpu_relax();
  166. }
  167. }
  168. /**
  169. * sysfs_put_active - put an active reference to sysfs_dirent
  170. * @sd: sysfs_dirent to put an active reference to
  171. *
  172. * Put an active reference to @sd. This function is noop if @sd
  173. * is NULL.
  174. */
  175. void sysfs_put_active(struct sysfs_dirent *sd)
  176. {
  177. struct completion *cmpl;
  178. int v;
  179. if (unlikely(!sd))
  180. return;
  181. v = atomic_dec_return(&sd->s_active);
  182. if (likely(v != SD_DEACTIVATED_BIAS))
  183. return;
  184. /* atomic_dec_return() is a mb(), we'll always see the updated
  185. * sd->s_sibling.
  186. */
  187. cmpl = (void *)sd->s_sibling;
  188. complete(cmpl);
  189. }
  190. /**
  191. * sysfs_get_active_two - get active references to sysfs_dirent and parent
  192. * @sd: sysfs_dirent of interest
  193. *
  194. * Get active reference to @sd and its parent. Parent's active
  195. * reference is grabbed first. This function is noop if @sd is
  196. * NULL.
  197. *
  198. * RETURNS:
  199. * Pointer to @sd on success, NULL on failure.
  200. */
  201. struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd)
  202. {
  203. if (sd) {
  204. if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent)))
  205. return NULL;
  206. if (unlikely(!sysfs_get_active(sd))) {
  207. sysfs_put_active(sd->s_parent);
  208. return NULL;
  209. }
  210. }
  211. return sd;
  212. }
  213. /**
  214. * sysfs_put_active_two - put active references to sysfs_dirent and parent
  215. * @sd: sysfs_dirent of interest
  216. *
  217. * Put active references to @sd and its parent. This function is
  218. * noop if @sd is NULL.
  219. */
  220. void sysfs_put_active_two(struct sysfs_dirent *sd)
  221. {
  222. if (sd) {
  223. sysfs_put_active(sd);
  224. sysfs_put_active(sd->s_parent);
  225. }
  226. }
  227. /**
  228. * sysfs_deactivate - deactivate sysfs_dirent
  229. * @sd: sysfs_dirent to deactivate
  230. *
  231. * Deny new active references and drain existing ones.
  232. */
  233. static void sysfs_deactivate(struct sysfs_dirent *sd)
  234. {
  235. DECLARE_COMPLETION_ONSTACK(wait);
  236. int v;
  237. BUG_ON(sd->s_sibling || !(sd->s_flags & SYSFS_FLAG_REMOVED));
  238. sd->s_sibling = (void *)&wait;
  239. /* atomic_add_return() is a mb(), put_active() will always see
  240. * the updated sd->s_sibling.
  241. */
  242. v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
  243. if (v != SD_DEACTIVATED_BIAS)
  244. wait_for_completion(&wait);
  245. sd->s_sibling = NULL;
  246. }
  247. static int sysfs_alloc_ino(ino_t *pino)
  248. {
  249. int ino, rc;
  250. retry:
  251. spin_lock(&sysfs_ino_lock);
  252. rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
  253. spin_unlock(&sysfs_ino_lock);
  254. if (rc == -EAGAIN) {
  255. if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
  256. goto retry;
  257. rc = -ENOMEM;
  258. }
  259. *pino = ino;
  260. return rc;
  261. }
  262. static void sysfs_free_ino(ino_t ino)
  263. {
  264. spin_lock(&sysfs_ino_lock);
  265. ida_remove(&sysfs_ino_ida, ino);
  266. spin_unlock(&sysfs_ino_lock);
  267. }
  268. void release_sysfs_dirent(struct sysfs_dirent * sd)
  269. {
  270. struct sysfs_dirent *parent_sd;
  271. repeat:
  272. /* Moving/renaming is always done while holding reference.
  273. * sd->s_parent won't change beneath us.
  274. */
  275. parent_sd = sd->s_parent;
  276. if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
  277. sysfs_put(sd->s_elem.symlink.target_sd);
  278. if (sysfs_type(sd) & SYSFS_COPY_NAME)
  279. kfree(sd->s_name);
  280. kfree(sd->s_iattr);
  281. sysfs_free_ino(sd->s_ino);
  282. kmem_cache_free(sysfs_dir_cachep, sd);
  283. sd = parent_sd;
  284. if (sd && atomic_dec_and_test(&sd->s_count))
  285. goto repeat;
  286. }
  287. static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
  288. {
  289. struct sysfs_dirent * sd = dentry->d_fsdata;
  290. if (sd) {
  291. /* sd->s_dentry is protected with sysfs_assoc_lock.
  292. * This allows sysfs_drop_dentry() to dereference it.
  293. */
  294. spin_lock(&sysfs_assoc_lock);
  295. /* The dentry might have been deleted or another
  296. * lookup could have happened updating sd->s_dentry to
  297. * point the new dentry. Ignore if it isn't pointing
  298. * to this dentry.
  299. */
  300. if (sd->s_dentry == dentry)
  301. sd->s_dentry = NULL;
  302. spin_unlock(&sysfs_assoc_lock);
  303. sysfs_put(sd);
  304. }
  305. iput(inode);
  306. }
  307. static struct dentry_operations sysfs_dentry_ops = {
  308. .d_iput = sysfs_d_iput,
  309. };
  310. struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
  311. {
  312. char *dup_name = NULL;
  313. struct sysfs_dirent *sd;
  314. if (type & SYSFS_COPY_NAME) {
  315. name = dup_name = kstrdup(name, GFP_KERNEL);
  316. if (!name)
  317. return NULL;
  318. }
  319. sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
  320. if (!sd)
  321. goto err_out1;
  322. if (sysfs_alloc_ino(&sd->s_ino))
  323. goto err_out2;
  324. atomic_set(&sd->s_count, 1);
  325. atomic_set(&sd->s_active, 0);
  326. atomic_set(&sd->s_event, 1);
  327. sd->s_name = name;
  328. sd->s_mode = mode;
  329. sd->s_flags = type;
  330. return sd;
  331. err_out2:
  332. kmem_cache_free(sysfs_dir_cachep, sd);
  333. err_out1:
  334. kfree(dup_name);
  335. return NULL;
  336. }
  337. /**
  338. * sysfs_attach_dentry - associate sysfs_dirent with dentry
  339. * @sd: target sysfs_dirent
  340. * @dentry: dentry to associate
  341. *
  342. * Associate @sd with @dentry. This is protected by
  343. * sysfs_assoc_lock to avoid race with sysfs_d_iput().
  344. *
  345. * LOCKING:
  346. * mutex_lock(sysfs_mutex)
  347. */
  348. static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry)
  349. {
  350. dentry->d_op = &sysfs_dentry_ops;
  351. dentry->d_fsdata = sysfs_get(sd);
  352. /* protect sd->s_dentry against sysfs_d_iput */
  353. spin_lock(&sysfs_assoc_lock);
  354. sd->s_dentry = dentry;
  355. spin_unlock(&sysfs_assoc_lock);
  356. d_rehash(dentry);
  357. }
  358. static int sysfs_ilookup_test(struct inode *inode, void *arg)
  359. {
  360. struct sysfs_dirent *sd = arg;
  361. return inode->i_ino == sd->s_ino;
  362. }
  363. /**
  364. * sysfs_addrm_start - prepare for sysfs_dirent add/remove
  365. * @acxt: pointer to sysfs_addrm_cxt to be used
  366. * @parent_sd: parent sysfs_dirent
  367. *
  368. * This function is called when the caller is about to add or
  369. * remove sysfs_dirent under @parent_sd. This function acquires
  370. * sysfs_mutex, grabs inode for @parent_sd if available and lock
  371. * i_mutex of it. @acxt is used to keep and pass context to
  372. * other addrm functions.
  373. *
  374. * LOCKING:
  375. * Kernel thread context (may sleep). sysfs_mutex is locked on
  376. * return. i_mutex of parent inode is locked on return if
  377. * available.
  378. */
  379. void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt,
  380. struct sysfs_dirent *parent_sd)
  381. {
  382. struct inode *inode;
  383. memset(acxt, 0, sizeof(*acxt));
  384. acxt->parent_sd = parent_sd;
  385. /* Lookup parent inode. inode initialization and I_NEW
  386. * clearing are protected by sysfs_mutex. By grabbing it and
  387. * looking up with _nowait variant, inode state can be
  388. * determined reliably.
  389. */
  390. mutex_lock(&sysfs_mutex);
  391. inode = ilookup5_nowait(sysfs_sb, parent_sd->s_ino, sysfs_ilookup_test,
  392. parent_sd);
  393. if (inode && !(inode->i_state & I_NEW)) {
  394. /* parent inode available */
  395. acxt->parent_inode = inode;
  396. /* sysfs_mutex is below i_mutex in lock hierarchy.
  397. * First, trylock i_mutex. If fails, unlock
  398. * sysfs_mutex and lock them in order.
  399. */
  400. if (!mutex_trylock(&inode->i_mutex)) {
  401. mutex_unlock(&sysfs_mutex);
  402. mutex_lock(&inode->i_mutex);
  403. mutex_lock(&sysfs_mutex);
  404. }
  405. } else
  406. iput(inode);
  407. }
  408. /**
  409. * sysfs_add_one - add sysfs_dirent to parent
  410. * @acxt: addrm context to use
  411. * @sd: sysfs_dirent to be added
  412. *
  413. * Get @acxt->parent_sd and set sd->s_parent to it and increment
  414. * nlink of parent inode if @sd is a directory. @sd is NOT
  415. * linked into the children list of the parent. The caller
  416. * should invoke sysfs_link_sibling() after this function
  417. * completes if @sd needs to be on the children list.
  418. *
  419. * This function should be called between calls to
  420. * sysfs_addrm_start() and sysfs_addrm_finish() and should be
  421. * passed the same @acxt as passed to sysfs_addrm_start().
  422. *
  423. * LOCKING:
  424. * Determined by sysfs_addrm_start().
  425. *
  426. * RETURNS:
  427. * 0 on success, -EEXIST if entry with the given name already
  428. * exists.
  429. */
  430. int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
  431. {
  432. if (sysfs_find_dirent(acxt->parent_sd, sd->s_name))
  433. return -EEXIST;
  434. sd->s_parent = sysfs_get(acxt->parent_sd);
  435. if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
  436. inc_nlink(acxt->parent_inode);
  437. acxt->cnt++;
  438. sysfs_link_sibling(sd);
  439. return 0;
  440. }
  441. /**
  442. * sysfs_remove_one - remove sysfs_dirent from parent
  443. * @acxt: addrm context to use
  444. * @sd: sysfs_dirent to be added
  445. *
  446. * Mark @sd removed and drop nlink of parent inode if @sd is a
  447. * directory. @sd is NOT unlinked from the children list of the
  448. * parent. The caller is repsonsible for removing @sd from the
  449. * children list before calling this function.
  450. *
  451. * This function should be called between calls to
  452. * sysfs_addrm_start() and sysfs_addrm_finish() and should be
  453. * passed the same @acxt as passed to sysfs_addrm_start().
  454. *
  455. * LOCKING:
  456. * Determined by sysfs_addrm_start().
  457. */
  458. void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
  459. {
  460. BUG_ON(sd->s_flags & SYSFS_FLAG_REMOVED);
  461. sysfs_unlink_sibling(sd);
  462. sd->s_flags |= SYSFS_FLAG_REMOVED;
  463. sd->s_sibling = acxt->removed;
  464. acxt->removed = sd;
  465. if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
  466. drop_nlink(acxt->parent_inode);
  467. acxt->cnt++;
  468. }
  469. /**
  470. * sysfs_drop_dentry - drop dentry for the specified sysfs_dirent
  471. * @sd: target sysfs_dirent
  472. *
  473. * Drop dentry for @sd. @sd must have been unlinked from its
  474. * parent on entry to this function such that it can't be looked
  475. * up anymore.
  476. *
  477. * @sd->s_dentry which is protected with sysfs_assoc_lock points
  478. * to the currently associated dentry but we're not holding a
  479. * reference to it and racing with dput(). Grab dcache_lock and
  480. * verify dentry before dropping it. If @sd->s_dentry is NULL or
  481. * dput() beats us, no need to bother.
  482. */
  483. static void sysfs_drop_dentry(struct sysfs_dirent *sd)
  484. {
  485. struct dentry *dentry = NULL;
  486. struct inode *inode;
  487. /* We're not holding a reference to ->s_dentry dentry but the
  488. * field will stay valid as long as sysfs_assoc_lock is held.
  489. */
  490. spin_lock(&sysfs_assoc_lock);
  491. spin_lock(&dcache_lock);
  492. /* drop dentry if it's there and dput() didn't kill it yet */
  493. if (sd->s_dentry && sd->s_dentry->d_inode) {
  494. dentry = dget_locked(sd->s_dentry);
  495. spin_lock(&dentry->d_lock);
  496. __d_drop(dentry);
  497. spin_unlock(&dentry->d_lock);
  498. }
  499. spin_unlock(&dcache_lock);
  500. spin_unlock(&sysfs_assoc_lock);
  501. dput(dentry);
  502. /* adjust nlink and update timestamp */
  503. inode = ilookup(sysfs_sb, sd->s_ino);
  504. if (inode) {
  505. mutex_lock(&inode->i_mutex);
  506. inode->i_ctime = CURRENT_TIME;
  507. drop_nlink(inode);
  508. if (sysfs_type(sd) == SYSFS_DIR)
  509. drop_nlink(inode);
  510. mutex_unlock(&inode->i_mutex);
  511. iput(inode);
  512. }
  513. }
  514. /**
  515. * sysfs_addrm_finish - finish up sysfs_dirent add/remove
  516. * @acxt: addrm context to finish up
  517. *
  518. * Finish up sysfs_dirent add/remove. Resources acquired by
  519. * sysfs_addrm_start() are released and removed sysfs_dirents are
  520. * cleaned up. Timestamps on the parent inode are updated.
  521. *
  522. * LOCKING:
  523. * All mutexes acquired by sysfs_addrm_start() are released.
  524. */
  525. void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
  526. {
  527. /* release resources acquired by sysfs_addrm_start() */
  528. mutex_unlock(&sysfs_mutex);
  529. if (acxt->parent_inode) {
  530. struct inode *inode = acxt->parent_inode;
  531. /* if added/removed, update timestamps on the parent */
  532. if (acxt->cnt)
  533. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  534. mutex_unlock(&inode->i_mutex);
  535. iput(inode);
  536. }
  537. /* kill removed sysfs_dirents */
  538. while (acxt->removed) {
  539. struct sysfs_dirent *sd = acxt->removed;
  540. acxt->removed = sd->s_sibling;
  541. sd->s_sibling = NULL;
  542. sysfs_drop_dentry(sd);
  543. sysfs_deactivate(sd);
  544. sysfs_put(sd);
  545. }
  546. }
  547. /**
  548. * sysfs_find_dirent - find sysfs_dirent with the given name
  549. * @parent_sd: sysfs_dirent to search under
  550. * @name: name to look for
  551. *
  552. * Look for sysfs_dirent with name @name under @parent_sd.
  553. *
  554. * LOCKING:
  555. * mutex_lock(sysfs_mutex)
  556. *
  557. * RETURNS:
  558. * Pointer to sysfs_dirent if found, NULL if not.
  559. */
  560. struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
  561. const unsigned char *name)
  562. {
  563. struct sysfs_dirent *sd;
  564. for (sd = parent_sd->s_children; sd; sd = sd->s_sibling)
  565. if (sysfs_type(sd) && !strcmp(sd->s_name, name))
  566. return sd;
  567. return NULL;
  568. }
  569. /**
  570. * sysfs_get_dirent - find and get sysfs_dirent with the given name
  571. * @parent_sd: sysfs_dirent to search under
  572. * @name: name to look for
  573. *
  574. * Look for sysfs_dirent with name @name under @parent_sd and get
  575. * it if found.
  576. *
  577. * LOCKING:
  578. * Kernel thread context (may sleep). Grabs sysfs_mutex.
  579. *
  580. * RETURNS:
  581. * Pointer to sysfs_dirent if found, NULL if not.
  582. */
  583. struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
  584. const unsigned char *name)
  585. {
  586. struct sysfs_dirent *sd;
  587. mutex_lock(&sysfs_mutex);
  588. sd = sysfs_find_dirent(parent_sd, name);
  589. sysfs_get(sd);
  590. mutex_unlock(&sysfs_mutex);
  591. return sd;
  592. }
  593. static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
  594. const char *name, struct sysfs_dirent **p_sd)
  595. {
  596. umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
  597. struct sysfs_addrm_cxt acxt;
  598. struct sysfs_dirent *sd;
  599. int rc;
  600. /* allocate */
  601. sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
  602. if (!sd)
  603. return -ENOMEM;
  604. sd->s_elem.dir.kobj = kobj;
  605. /* link in */
  606. sysfs_addrm_start(&acxt, parent_sd);
  607. rc = sysfs_add_one(&acxt, sd);
  608. sysfs_addrm_finish(&acxt);
  609. if (rc == 0)
  610. *p_sd = sd;
  611. else
  612. sysfs_put(sd);
  613. return rc;
  614. }
  615. int sysfs_create_subdir(struct kobject *kobj, const char *name,
  616. struct sysfs_dirent **p_sd)
  617. {
  618. return create_dir(kobj, kobj->sd, name, p_sd);
  619. }
  620. /**
  621. * sysfs_create_dir - create a directory for an object.
  622. * @kobj: object we're creating directory for.
  623. */
  624. int sysfs_create_dir(struct kobject * kobj)
  625. {
  626. struct sysfs_dirent *parent_sd, *sd;
  627. int error = 0;
  628. BUG_ON(!kobj);
  629. if (kobj->parent)
  630. parent_sd = kobj->parent->sd;
  631. else
  632. parent_sd = &sysfs_root;
  633. error = create_dir(kobj, parent_sd, kobject_name(kobj), &sd);
  634. if (!error)
  635. kobj->sd = sd;
  636. return error;
  637. }
  638. static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
  639. struct nameidata *nd)
  640. {
  641. struct dentry *ret = NULL;
  642. struct sysfs_dirent *parent_sd = dentry->d_parent->d_fsdata;
  643. struct sysfs_dirent *sd;
  644. struct inode *inode;
  645. mutex_lock(&sysfs_mutex);
  646. for (sd = parent_sd->s_children; sd; sd = sd->s_sibling)
  647. if (sysfs_type(sd) && !strcmp(sd->s_name, dentry->d_name.name))
  648. break;
  649. /* no such entry */
  650. if (!sd)
  651. goto out_unlock;
  652. /* attach dentry and inode */
  653. inode = sysfs_get_inode(sd);
  654. if (!inode) {
  655. ret = ERR_PTR(-ENOMEM);
  656. goto out_unlock;
  657. }
  658. d_instantiate(dentry, inode);
  659. sysfs_attach_dentry(sd, dentry);
  660. out_unlock:
  661. mutex_unlock(&sysfs_mutex);
  662. return ret;
  663. }
  664. const struct inode_operations sysfs_dir_inode_operations = {
  665. .lookup = sysfs_lookup,
  666. .setattr = sysfs_setattr,
  667. };
  668. static void remove_dir(struct sysfs_dirent *sd)
  669. {
  670. struct sysfs_addrm_cxt acxt;
  671. sysfs_addrm_start(&acxt, sd->s_parent);
  672. sysfs_remove_one(&acxt, sd);
  673. sysfs_addrm_finish(&acxt);
  674. }
  675. void sysfs_remove_subdir(struct sysfs_dirent *sd)
  676. {
  677. remove_dir(sd);
  678. }
  679. static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
  680. {
  681. struct sysfs_addrm_cxt acxt;
  682. struct sysfs_dirent **pos;
  683. if (!dir_sd)
  684. return;
  685. pr_debug("sysfs %s: removing dir\n", dir_sd->s_name);
  686. sysfs_addrm_start(&acxt, dir_sd);
  687. pos = &dir_sd->s_children;
  688. while (*pos) {
  689. struct sysfs_dirent *sd = *pos;
  690. if (sysfs_type(sd) && sysfs_type(sd) != SYSFS_DIR)
  691. sysfs_remove_one(&acxt, sd);
  692. else
  693. pos = &(*pos)->s_sibling;
  694. }
  695. sysfs_addrm_finish(&acxt);
  696. remove_dir(dir_sd);
  697. }
  698. /**
  699. * sysfs_remove_dir - remove an object's directory.
  700. * @kobj: object.
  701. *
  702. * The only thing special about this is that we remove any files in
  703. * the directory before we remove the directory, and we've inlined
  704. * what used to be sysfs_rmdir() below, instead of calling separately.
  705. */
  706. void sysfs_remove_dir(struct kobject * kobj)
  707. {
  708. struct sysfs_dirent *sd = kobj->sd;
  709. spin_lock(&sysfs_assoc_lock);
  710. kobj->sd = NULL;
  711. spin_unlock(&sysfs_assoc_lock);
  712. __sysfs_remove_dir(sd);
  713. }
  714. int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
  715. {
  716. struct sysfs_dirent *sd;
  717. struct dentry *parent = NULL;
  718. struct dentry *old_dentry = NULL, *new_dentry = NULL;
  719. const char *dup_name = NULL;
  720. int error;
  721. /* get the original dentry */
  722. sd = kobj->sd;
  723. old_dentry = sysfs_get_dentry(sd);
  724. if (IS_ERR(old_dentry)) {
  725. error = PTR_ERR(old_dentry);
  726. goto out_dput;
  727. }
  728. parent = old_dentry->d_parent;
  729. /* lock parent and get dentry for new name */
  730. mutex_lock(&parent->d_inode->i_mutex);
  731. new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
  732. if (IS_ERR(new_dentry)) {
  733. error = PTR_ERR(new_dentry);
  734. goto out_unlock;
  735. }
  736. error = -EINVAL;
  737. if (old_dentry == new_dentry)
  738. goto out_unlock;
  739. error = -EEXIST;
  740. if (new_dentry->d_inode)
  741. goto out_unlock;
  742. /* rename kobject and sysfs_dirent */
  743. error = -ENOMEM;
  744. new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
  745. if (!new_name)
  746. goto out_drop;
  747. error = kobject_set_name(kobj, "%s", new_name);
  748. if (error)
  749. goto out_drop;
  750. mutex_lock(&sysfs_mutex);
  751. dup_name = sd->s_name;
  752. sd->s_name = new_name;
  753. mutex_unlock(&sysfs_mutex);
  754. /* rename */
  755. d_add(new_dentry, NULL);
  756. d_move(sd->s_dentry, new_dentry);
  757. error = 0;
  758. goto out_unlock;
  759. out_drop:
  760. d_drop(new_dentry);
  761. out_unlock:
  762. mutex_unlock(&parent->d_inode->i_mutex);
  763. out_dput:
  764. kfree(dup_name);
  765. dput(old_dentry);
  766. dput(new_dentry);
  767. return error;
  768. }
  769. int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj)
  770. {
  771. struct sysfs_dirent *sd = kobj->sd;
  772. struct sysfs_dirent *new_parent_sd;
  773. struct dentry *old_parent, *new_parent = NULL;
  774. struct dentry *old_dentry = NULL, *new_dentry = NULL;
  775. int error;
  776. BUG_ON(!sd->s_parent);
  777. new_parent_sd = new_parent_kobj->sd ? new_parent_kobj->sd : &sysfs_root;
  778. /* get dentries */
  779. old_dentry = sysfs_get_dentry(sd);
  780. if (IS_ERR(old_dentry)) {
  781. error = PTR_ERR(old_dentry);
  782. goto out_dput;
  783. }
  784. old_parent = sd->s_parent->s_dentry;
  785. new_parent = sysfs_get_dentry(new_parent_sd);
  786. if (IS_ERR(new_parent)) {
  787. error = PTR_ERR(new_parent);
  788. goto out_dput;
  789. }
  790. if (old_parent->d_inode == new_parent->d_inode) {
  791. error = 0;
  792. goto out_dput; /* nothing to move */
  793. }
  794. again:
  795. mutex_lock(&old_parent->d_inode->i_mutex);
  796. if (!mutex_trylock(&new_parent->d_inode->i_mutex)) {
  797. mutex_unlock(&old_parent->d_inode->i_mutex);
  798. goto again;
  799. }
  800. new_dentry = lookup_one_len(kobject_name(kobj), new_parent, strlen(kobject_name(kobj)));
  801. if (IS_ERR(new_dentry)) {
  802. error = PTR_ERR(new_dentry);
  803. goto out_unlock;
  804. } else
  805. error = 0;
  806. d_add(new_dentry, NULL);
  807. d_move(sd->s_dentry, new_dentry);
  808. dput(new_dentry);
  809. /* Remove from old parent's list and insert into new parent's list. */
  810. mutex_lock(&sysfs_mutex);
  811. sysfs_unlink_sibling(sd);
  812. sysfs_get(new_parent_sd);
  813. sysfs_put(sd->s_parent);
  814. sd->s_parent = new_parent_sd;
  815. sysfs_link_sibling(sd);
  816. mutex_unlock(&sysfs_mutex);
  817. out_unlock:
  818. mutex_unlock(&new_parent->d_inode->i_mutex);
  819. mutex_unlock(&old_parent->d_inode->i_mutex);
  820. out_dput:
  821. dput(new_parent);
  822. dput(old_dentry);
  823. dput(new_dentry);
  824. return error;
  825. }
  826. static int sysfs_dir_open(struct inode *inode, struct file *file)
  827. {
  828. struct dentry * dentry = file->f_path.dentry;
  829. struct sysfs_dirent * parent_sd = dentry->d_fsdata;
  830. struct sysfs_dirent * sd;
  831. sd = sysfs_new_dirent("_DIR_", 0, 0);
  832. if (sd) {
  833. mutex_lock(&sysfs_mutex);
  834. sd->s_parent = sysfs_get(parent_sd);
  835. sysfs_link_sibling(sd);
  836. mutex_unlock(&sysfs_mutex);
  837. }
  838. file->private_data = sd;
  839. return sd ? 0 : -ENOMEM;
  840. }
  841. static int sysfs_dir_close(struct inode *inode, struct file *file)
  842. {
  843. struct sysfs_dirent * cursor = file->private_data;
  844. mutex_lock(&sysfs_mutex);
  845. sysfs_unlink_sibling(cursor);
  846. mutex_unlock(&sysfs_mutex);
  847. release_sysfs_dirent(cursor);
  848. return 0;
  849. }
  850. /* Relationship between s_mode and the DT_xxx types */
  851. static inline unsigned char dt_type(struct sysfs_dirent *sd)
  852. {
  853. return (sd->s_mode >> 12) & 15;
  854. }
  855. static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
  856. {
  857. struct dentry *dentry = filp->f_path.dentry;
  858. struct sysfs_dirent * parent_sd = dentry->d_fsdata;
  859. struct sysfs_dirent *cursor = filp->private_data;
  860. struct sysfs_dirent **pos;
  861. ino_t ino;
  862. int i = filp->f_pos;
  863. switch (i) {
  864. case 0:
  865. ino = parent_sd->s_ino;
  866. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  867. break;
  868. filp->f_pos++;
  869. i++;
  870. /* fallthrough */
  871. case 1:
  872. if (parent_sd->s_parent)
  873. ino = parent_sd->s_parent->s_ino;
  874. else
  875. ino = parent_sd->s_ino;
  876. if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
  877. break;
  878. filp->f_pos++;
  879. i++;
  880. /* fallthrough */
  881. default:
  882. mutex_lock(&sysfs_mutex);
  883. pos = &parent_sd->s_children;
  884. while (*pos != cursor)
  885. pos = &(*pos)->s_sibling;
  886. /* unlink cursor */
  887. *pos = cursor->s_sibling;
  888. if (filp->f_pos == 2)
  889. pos = &parent_sd->s_children;
  890. for ( ; *pos; pos = &(*pos)->s_sibling) {
  891. struct sysfs_dirent *next = *pos;
  892. const char * name;
  893. int len;
  894. if (!sysfs_type(next))
  895. continue;
  896. name = next->s_name;
  897. len = strlen(name);
  898. ino = next->s_ino;
  899. if (filldir(dirent, name, len, filp->f_pos, ino,
  900. dt_type(next)) < 0)
  901. break;
  902. filp->f_pos++;
  903. }
  904. /* put cursor back in */
  905. cursor->s_sibling = *pos;
  906. *pos = cursor;
  907. mutex_unlock(&sysfs_mutex);
  908. }
  909. return 0;
  910. }
  911. static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
  912. {
  913. struct dentry * dentry = file->f_path.dentry;
  914. switch (origin) {
  915. case 1:
  916. offset += file->f_pos;
  917. case 0:
  918. if (offset >= 0)
  919. break;
  920. default:
  921. return -EINVAL;
  922. }
  923. if (offset != file->f_pos) {
  924. mutex_lock(&sysfs_mutex);
  925. file->f_pos = offset;
  926. if (file->f_pos >= 2) {
  927. struct sysfs_dirent *sd = dentry->d_fsdata;
  928. struct sysfs_dirent *cursor = file->private_data;
  929. struct sysfs_dirent **pos;
  930. loff_t n = file->f_pos - 2;
  931. sysfs_unlink_sibling(cursor);
  932. pos = &sd->s_children;
  933. while (n && *pos) {
  934. struct sysfs_dirent *next = *pos;
  935. if (sysfs_type(next))
  936. n--;
  937. pos = &(*pos)->s_sibling;
  938. }
  939. cursor->s_sibling = *pos;
  940. *pos = cursor;
  941. }
  942. mutex_unlock(&sysfs_mutex);
  943. }
  944. return offset;
  945. }
  946. const struct file_operations sysfs_dir_operations = {
  947. .open = sysfs_dir_open,
  948. .release = sysfs_dir_close,
  949. .llseek = sysfs_dir_lseek,
  950. .read = generic_read_dir,
  951. .readdir = sysfs_readdir,
  952. };