dir.c 27 KB

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