dir.c 22 KB

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