dir.c 24 KB

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