dir.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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 if (sysfs_mount && sysfs_mount->mnt_sb)
  632. parent_sd = sysfs_mount->mnt_sb->s_root->d_fsdata;
  633. else
  634. return -EFAULT;
  635. error = create_dir(kobj, parent_sd, kobject_name(kobj), &sd);
  636. if (!error)
  637. kobj->sd = sd;
  638. return error;
  639. }
  640. static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
  641. struct nameidata *nd)
  642. {
  643. struct dentry *ret = NULL;
  644. struct sysfs_dirent *parent_sd = dentry->d_parent->d_fsdata;
  645. struct sysfs_dirent *sd;
  646. struct inode *inode;
  647. mutex_lock(&sysfs_mutex);
  648. for (sd = parent_sd->s_children; sd; sd = sd->s_sibling)
  649. if (sysfs_type(sd) && !strcmp(sd->s_name, dentry->d_name.name))
  650. break;
  651. /* no such entry */
  652. if (!sd)
  653. goto out_unlock;
  654. /* attach dentry and inode */
  655. inode = sysfs_get_inode(sd);
  656. if (!inode) {
  657. ret = ERR_PTR(-ENOMEM);
  658. goto out_unlock;
  659. }
  660. d_instantiate(dentry, inode);
  661. sysfs_attach_dentry(sd, dentry);
  662. out_unlock:
  663. mutex_unlock(&sysfs_mutex);
  664. return ret;
  665. }
  666. const struct inode_operations sysfs_dir_inode_operations = {
  667. .lookup = sysfs_lookup,
  668. .setattr = sysfs_setattr,
  669. };
  670. static void remove_dir(struct sysfs_dirent *sd)
  671. {
  672. struct sysfs_addrm_cxt acxt;
  673. sysfs_addrm_start(&acxt, sd->s_parent);
  674. sysfs_remove_one(&acxt, sd);
  675. sysfs_addrm_finish(&acxt);
  676. }
  677. void sysfs_remove_subdir(struct sysfs_dirent *sd)
  678. {
  679. remove_dir(sd);
  680. }
  681. static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
  682. {
  683. struct sysfs_addrm_cxt acxt;
  684. struct sysfs_dirent **pos;
  685. if (!dir_sd)
  686. return;
  687. pr_debug("sysfs %s: removing dir\n", dir_sd->s_name);
  688. sysfs_addrm_start(&acxt, dir_sd);
  689. pos = &dir_sd->s_children;
  690. while (*pos) {
  691. struct sysfs_dirent *sd = *pos;
  692. if (sysfs_type(sd) && sysfs_type(sd) != SYSFS_DIR)
  693. sysfs_remove_one(&acxt, sd);
  694. else
  695. pos = &(*pos)->s_sibling;
  696. }
  697. sysfs_addrm_finish(&acxt);
  698. remove_dir(dir_sd);
  699. }
  700. /**
  701. * sysfs_remove_dir - remove an object's directory.
  702. * @kobj: object.
  703. *
  704. * The only thing special about this is that we remove any files in
  705. * the directory before we remove the directory, and we've inlined
  706. * what used to be sysfs_rmdir() below, instead of calling separately.
  707. */
  708. void sysfs_remove_dir(struct kobject * kobj)
  709. {
  710. struct sysfs_dirent *sd = kobj->sd;
  711. spin_lock(&sysfs_assoc_lock);
  712. kobj->sd = NULL;
  713. spin_unlock(&sysfs_assoc_lock);
  714. __sysfs_remove_dir(sd);
  715. }
  716. int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
  717. {
  718. struct sysfs_dirent *sd;
  719. struct dentry *parent = NULL;
  720. struct dentry *old_dentry = NULL, *new_dentry = NULL;
  721. const char *dup_name = NULL;
  722. int error;
  723. /* get the original dentry */
  724. sd = kobj->sd;
  725. old_dentry = sysfs_get_dentry(sd);
  726. if (IS_ERR(old_dentry)) {
  727. error = PTR_ERR(old_dentry);
  728. goto out_dput;
  729. }
  730. parent = old_dentry->d_parent;
  731. /* lock parent and get dentry for new name */
  732. mutex_lock(&parent->d_inode->i_mutex);
  733. new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
  734. if (IS_ERR(new_dentry)) {
  735. error = PTR_ERR(new_dentry);
  736. goto out_unlock;
  737. }
  738. error = -EINVAL;
  739. if (old_dentry == new_dentry)
  740. goto out_unlock;
  741. error = -EEXIST;
  742. if (new_dentry->d_inode)
  743. goto out_unlock;
  744. /* rename kobject and sysfs_dirent */
  745. error = -ENOMEM;
  746. new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
  747. if (!new_name)
  748. goto out_drop;
  749. error = kobject_set_name(kobj, "%s", new_name);
  750. if (error)
  751. goto out_drop;
  752. mutex_lock(&sysfs_mutex);
  753. dup_name = sd->s_name;
  754. sd->s_name = new_name;
  755. mutex_unlock(&sysfs_mutex);
  756. /* rename */
  757. d_add(new_dentry, NULL);
  758. d_move(sd->s_dentry, new_dentry);
  759. error = 0;
  760. goto out_unlock;
  761. out_drop:
  762. d_drop(new_dentry);
  763. out_unlock:
  764. mutex_unlock(&parent->d_inode->i_mutex);
  765. out_dput:
  766. kfree(dup_name);
  767. dput(old_dentry);
  768. dput(new_dentry);
  769. return error;
  770. }
  771. int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj)
  772. {
  773. struct sysfs_dirent *sd = kobj->sd;
  774. struct sysfs_dirent *new_parent_sd;
  775. struct dentry *old_parent, *new_parent = NULL;
  776. struct dentry *old_dentry = NULL, *new_dentry = NULL;
  777. int error;
  778. BUG_ON(!sd->s_parent);
  779. new_parent_sd = new_parent_kobj->sd ? new_parent_kobj->sd : &sysfs_root;
  780. /* get dentries */
  781. old_dentry = sysfs_get_dentry(sd);
  782. if (IS_ERR(old_dentry)) {
  783. error = PTR_ERR(old_dentry);
  784. goto out_dput;
  785. }
  786. old_parent = sd->s_parent->s_dentry;
  787. new_parent = sysfs_get_dentry(new_parent_sd);
  788. if (IS_ERR(new_parent)) {
  789. error = PTR_ERR(new_parent);
  790. goto out_dput;
  791. }
  792. if (old_parent->d_inode == new_parent->d_inode) {
  793. error = 0;
  794. goto out_dput; /* nothing to move */
  795. }
  796. again:
  797. mutex_lock(&old_parent->d_inode->i_mutex);
  798. if (!mutex_trylock(&new_parent->d_inode->i_mutex)) {
  799. mutex_unlock(&old_parent->d_inode->i_mutex);
  800. goto again;
  801. }
  802. new_dentry = lookup_one_len(kobject_name(kobj), new_parent, strlen(kobject_name(kobj)));
  803. if (IS_ERR(new_dentry)) {
  804. error = PTR_ERR(new_dentry);
  805. goto out_unlock;
  806. } else
  807. error = 0;
  808. d_add(new_dentry, NULL);
  809. d_move(sd->s_dentry, new_dentry);
  810. dput(new_dentry);
  811. /* Remove from old parent's list and insert into new parent's list. */
  812. mutex_lock(&sysfs_mutex);
  813. sysfs_unlink_sibling(sd);
  814. sysfs_get(new_parent_sd);
  815. sysfs_put(sd->s_parent);
  816. sd->s_parent = new_parent_sd;
  817. sysfs_link_sibling(sd);
  818. mutex_unlock(&sysfs_mutex);
  819. out_unlock:
  820. mutex_unlock(&new_parent->d_inode->i_mutex);
  821. mutex_unlock(&old_parent->d_inode->i_mutex);
  822. out_dput:
  823. dput(new_parent);
  824. dput(old_dentry);
  825. dput(new_dentry);
  826. return error;
  827. }
  828. static int sysfs_dir_open(struct inode *inode, struct file *file)
  829. {
  830. struct dentry * dentry = file->f_path.dentry;
  831. struct sysfs_dirent * parent_sd = dentry->d_fsdata;
  832. struct sysfs_dirent * sd;
  833. sd = sysfs_new_dirent("_DIR_", 0, 0);
  834. if (sd) {
  835. mutex_lock(&sysfs_mutex);
  836. sd->s_parent = sysfs_get(parent_sd);
  837. sysfs_link_sibling(sd);
  838. mutex_unlock(&sysfs_mutex);
  839. }
  840. file->private_data = sd;
  841. return sd ? 0 : -ENOMEM;
  842. }
  843. static int sysfs_dir_close(struct inode *inode, struct file *file)
  844. {
  845. struct sysfs_dirent * cursor = file->private_data;
  846. mutex_lock(&sysfs_mutex);
  847. sysfs_unlink_sibling(cursor);
  848. mutex_unlock(&sysfs_mutex);
  849. release_sysfs_dirent(cursor);
  850. return 0;
  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 *cursor = filp->private_data;
  862. struct sysfs_dirent **pos;
  863. ino_t ino;
  864. int i = filp->f_pos;
  865. switch (i) {
  866. case 0:
  867. ino = parent_sd->s_ino;
  868. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  869. break;
  870. filp->f_pos++;
  871. i++;
  872. /* fallthrough */
  873. case 1:
  874. if (parent_sd->s_parent)
  875. ino = parent_sd->s_parent->s_ino;
  876. else
  877. ino = parent_sd->s_ino;
  878. if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
  879. break;
  880. filp->f_pos++;
  881. i++;
  882. /* fallthrough */
  883. default:
  884. mutex_lock(&sysfs_mutex);
  885. pos = &parent_sd->s_children;
  886. while (*pos != cursor)
  887. pos = &(*pos)->s_sibling;
  888. /* unlink cursor */
  889. *pos = cursor->s_sibling;
  890. if (filp->f_pos == 2)
  891. pos = &parent_sd->s_children;
  892. for ( ; *pos; pos = &(*pos)->s_sibling) {
  893. struct sysfs_dirent *next = *pos;
  894. const char * name;
  895. int len;
  896. if (!sysfs_type(next))
  897. continue;
  898. name = next->s_name;
  899. len = strlen(name);
  900. ino = next->s_ino;
  901. if (filldir(dirent, name, len, filp->f_pos, ino,
  902. dt_type(next)) < 0)
  903. break;
  904. filp->f_pos++;
  905. }
  906. /* put cursor back in */
  907. cursor->s_sibling = *pos;
  908. *pos = cursor;
  909. mutex_unlock(&sysfs_mutex);
  910. }
  911. return 0;
  912. }
  913. static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
  914. {
  915. struct dentry * dentry = file->f_path.dentry;
  916. switch (origin) {
  917. case 1:
  918. offset += file->f_pos;
  919. case 0:
  920. if (offset >= 0)
  921. break;
  922. default:
  923. return -EINVAL;
  924. }
  925. if (offset != file->f_pos) {
  926. mutex_lock(&sysfs_mutex);
  927. file->f_pos = offset;
  928. if (file->f_pos >= 2) {
  929. struct sysfs_dirent *sd = dentry->d_fsdata;
  930. struct sysfs_dirent *cursor = file->private_data;
  931. struct sysfs_dirent **pos;
  932. loff_t n = file->f_pos - 2;
  933. sysfs_unlink_sibling(cursor);
  934. pos = &sd->s_children;
  935. while (n && *pos) {
  936. struct sysfs_dirent *next = *pos;
  937. if (sysfs_type(next))
  938. n--;
  939. pos = &(*pos)->s_sibling;
  940. }
  941. cursor->s_sibling = *pos;
  942. *pos = cursor;
  943. }
  944. mutex_unlock(&sysfs_mutex);
  945. }
  946. return offset;
  947. }
  948. const struct file_operations sysfs_dir_operations = {
  949. .open = sysfs_dir_open,
  950. .release = sysfs_dir_close,
  951. .llseek = sysfs_dir_lseek,
  952. .read = generic_read_dir,
  953. .readdir = sysfs_readdir,
  954. };