dir.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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 and link into the
  355. * children list of the parent.
  356. *
  357. * This function should be called between calls to
  358. * sysfs_addrm_start() and sysfs_addrm_finish() and should be
  359. * passed the same @acxt as passed to sysfs_addrm_start().
  360. *
  361. * LOCKING:
  362. * Determined by sysfs_addrm_start().
  363. *
  364. * RETURNS:
  365. * 0 on success, -EEXIST if entry with the given name already
  366. * exists.
  367. */
  368. int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
  369. {
  370. if (sysfs_find_dirent(acxt->parent_sd, sd->s_name)) {
  371. printk(KERN_WARNING "sysfs: duplicate filename '%s' "
  372. "can not be created\n", sd->s_name);
  373. WARN_ON(1);
  374. return -EEXIST;
  375. }
  376. sd->s_parent = sysfs_get(acxt->parent_sd);
  377. if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
  378. inc_nlink(acxt->parent_inode);
  379. acxt->cnt++;
  380. sysfs_link_sibling(sd);
  381. return 0;
  382. }
  383. /**
  384. * sysfs_remove_one - remove sysfs_dirent from parent
  385. * @acxt: addrm context to use
  386. * @sd: sysfs_dirent to be added
  387. *
  388. * Mark @sd removed and drop nlink of parent inode if @sd is a
  389. * directory. @sd is unlinked from the children list.
  390. *
  391. * This function should be called between calls to
  392. * sysfs_addrm_start() and sysfs_addrm_finish() and should be
  393. * passed the same @acxt as passed to sysfs_addrm_start().
  394. *
  395. * LOCKING:
  396. * Determined by sysfs_addrm_start().
  397. */
  398. void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
  399. {
  400. BUG_ON(sd->s_flags & SYSFS_FLAG_REMOVED);
  401. sysfs_unlink_sibling(sd);
  402. sd->s_flags |= SYSFS_FLAG_REMOVED;
  403. sd->s_sibling = acxt->removed;
  404. acxt->removed = sd;
  405. if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
  406. drop_nlink(acxt->parent_inode);
  407. acxt->cnt++;
  408. }
  409. /**
  410. * sysfs_drop_dentry - drop dentry for the specified sysfs_dirent
  411. * @sd: target sysfs_dirent
  412. *
  413. * Drop dentry for @sd. @sd must have been unlinked from its
  414. * parent on entry to this function such that it can't be looked
  415. * up anymore.
  416. */
  417. static void sysfs_drop_dentry(struct sysfs_dirent *sd)
  418. {
  419. struct inode *inode;
  420. struct dentry *dentry;
  421. inode = ilookup(sysfs_sb, sd->s_ino);
  422. if (!inode)
  423. return;
  424. /* Drop any existing dentries associated with sd.
  425. *
  426. * For the dentry to be properly freed we need to grab a
  427. * reference to the dentry under the dcache lock, unhash it,
  428. * and then put it. The playing with the dentry count allows
  429. * dput to immediately free the dentry if it is not in use.
  430. */
  431. repeat:
  432. spin_lock(&dcache_lock);
  433. list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
  434. if (d_unhashed(dentry))
  435. continue;
  436. dget_locked(dentry);
  437. spin_lock(&dentry->d_lock);
  438. __d_drop(dentry);
  439. spin_unlock(&dentry->d_lock);
  440. spin_unlock(&dcache_lock);
  441. dput(dentry);
  442. goto repeat;
  443. }
  444. spin_unlock(&dcache_lock);
  445. /* adjust nlink and update timestamp */
  446. mutex_lock(&inode->i_mutex);
  447. inode->i_ctime = CURRENT_TIME;
  448. drop_nlink(inode);
  449. if (sysfs_type(sd) == SYSFS_DIR)
  450. drop_nlink(inode);
  451. mutex_unlock(&inode->i_mutex);
  452. iput(inode);
  453. }
  454. /**
  455. * sysfs_addrm_finish - finish up sysfs_dirent add/remove
  456. * @acxt: addrm context to finish up
  457. *
  458. * Finish up sysfs_dirent add/remove. Resources acquired by
  459. * sysfs_addrm_start() are released and removed sysfs_dirents are
  460. * cleaned up. Timestamps on the parent inode are updated.
  461. *
  462. * LOCKING:
  463. * All mutexes acquired by sysfs_addrm_start() are released.
  464. */
  465. void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
  466. {
  467. /* release resources acquired by sysfs_addrm_start() */
  468. mutex_unlock(&sysfs_mutex);
  469. if (acxt->parent_inode) {
  470. struct inode *inode = acxt->parent_inode;
  471. /* if added/removed, update timestamps on the parent */
  472. if (acxt->cnt)
  473. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  474. mutex_unlock(&inode->i_mutex);
  475. iput(inode);
  476. }
  477. /* kill removed sysfs_dirents */
  478. while (acxt->removed) {
  479. struct sysfs_dirent *sd = acxt->removed;
  480. acxt->removed = sd->s_sibling;
  481. sd->s_sibling = NULL;
  482. sysfs_drop_dentry(sd);
  483. sysfs_deactivate(sd);
  484. sysfs_put(sd);
  485. }
  486. }
  487. /**
  488. * sysfs_find_dirent - find sysfs_dirent with the given name
  489. * @parent_sd: sysfs_dirent to search under
  490. * @name: name to look for
  491. *
  492. * Look for sysfs_dirent with name @name under @parent_sd.
  493. *
  494. * LOCKING:
  495. * mutex_lock(sysfs_mutex)
  496. *
  497. * RETURNS:
  498. * Pointer to sysfs_dirent if found, NULL if not.
  499. */
  500. struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
  501. const unsigned char *name)
  502. {
  503. struct sysfs_dirent *sd;
  504. for (sd = parent_sd->s_children; sd; sd = sd->s_sibling)
  505. if (!strcmp(sd->s_name, name))
  506. return sd;
  507. return NULL;
  508. }
  509. /**
  510. * sysfs_get_dirent - find and get sysfs_dirent with the given name
  511. * @parent_sd: sysfs_dirent to search under
  512. * @name: name to look for
  513. *
  514. * Look for sysfs_dirent with name @name under @parent_sd and get
  515. * it if found.
  516. *
  517. * LOCKING:
  518. * Kernel thread context (may sleep). Grabs sysfs_mutex.
  519. *
  520. * RETURNS:
  521. * Pointer to sysfs_dirent if found, NULL if not.
  522. */
  523. struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
  524. const unsigned char *name)
  525. {
  526. struct sysfs_dirent *sd;
  527. mutex_lock(&sysfs_mutex);
  528. sd = sysfs_find_dirent(parent_sd, name);
  529. sysfs_get(sd);
  530. mutex_unlock(&sysfs_mutex);
  531. return sd;
  532. }
  533. static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
  534. const char *name, struct sysfs_dirent **p_sd)
  535. {
  536. umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
  537. struct sysfs_addrm_cxt acxt;
  538. struct sysfs_dirent *sd;
  539. int rc;
  540. /* allocate */
  541. sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
  542. if (!sd)
  543. return -ENOMEM;
  544. sd->s_elem.dir.kobj = kobj;
  545. /* link in */
  546. sysfs_addrm_start(&acxt, parent_sd);
  547. rc = sysfs_add_one(&acxt, sd);
  548. sysfs_addrm_finish(&acxt);
  549. if (rc == 0)
  550. *p_sd = sd;
  551. else
  552. sysfs_put(sd);
  553. return rc;
  554. }
  555. int sysfs_create_subdir(struct kobject *kobj, const char *name,
  556. struct sysfs_dirent **p_sd)
  557. {
  558. return create_dir(kobj, kobj->sd, name, p_sd);
  559. }
  560. /**
  561. * sysfs_create_dir - create a directory for an object.
  562. * @kobj: object we're creating directory for.
  563. */
  564. int sysfs_create_dir(struct kobject * kobj)
  565. {
  566. struct sysfs_dirent *parent_sd, *sd;
  567. int error = 0;
  568. BUG_ON(!kobj);
  569. if (kobj->parent)
  570. parent_sd = kobj->parent->sd;
  571. else
  572. parent_sd = &sysfs_root;
  573. error = create_dir(kobj, parent_sd, kobject_name(kobj), &sd);
  574. if (!error)
  575. kobj->sd = sd;
  576. return error;
  577. }
  578. static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
  579. struct nameidata *nd)
  580. {
  581. struct dentry *ret = NULL;
  582. struct sysfs_dirent *parent_sd = dentry->d_parent->d_fsdata;
  583. struct sysfs_dirent *sd;
  584. struct inode *inode;
  585. mutex_lock(&sysfs_mutex);
  586. sd = sysfs_find_dirent(parent_sd, dentry->d_name.name);
  587. /* no such entry */
  588. if (!sd)
  589. goto out_unlock;
  590. /* attach dentry and inode */
  591. inode = sysfs_get_inode(sd);
  592. if (!inode) {
  593. ret = ERR_PTR(-ENOMEM);
  594. goto out_unlock;
  595. }
  596. d_instantiate(dentry, inode);
  597. sysfs_attach_dentry(sd, dentry);
  598. out_unlock:
  599. mutex_unlock(&sysfs_mutex);
  600. return ret;
  601. }
  602. const struct inode_operations sysfs_dir_inode_operations = {
  603. .lookup = sysfs_lookup,
  604. .setattr = sysfs_setattr,
  605. };
  606. static void remove_dir(struct sysfs_dirent *sd)
  607. {
  608. struct sysfs_addrm_cxt acxt;
  609. sysfs_addrm_start(&acxt, sd->s_parent);
  610. sysfs_remove_one(&acxt, sd);
  611. sysfs_addrm_finish(&acxt);
  612. }
  613. void sysfs_remove_subdir(struct sysfs_dirent *sd)
  614. {
  615. remove_dir(sd);
  616. }
  617. static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
  618. {
  619. struct sysfs_addrm_cxt acxt;
  620. struct sysfs_dirent **pos;
  621. if (!dir_sd)
  622. return;
  623. pr_debug("sysfs %s: removing dir\n", dir_sd->s_name);
  624. sysfs_addrm_start(&acxt, dir_sd);
  625. pos = &dir_sd->s_children;
  626. while (*pos) {
  627. struct sysfs_dirent *sd = *pos;
  628. if (sysfs_type(sd) != SYSFS_DIR)
  629. sysfs_remove_one(&acxt, sd);
  630. else
  631. pos = &(*pos)->s_sibling;
  632. }
  633. sysfs_addrm_finish(&acxt);
  634. remove_dir(dir_sd);
  635. }
  636. /**
  637. * sysfs_remove_dir - remove an object's directory.
  638. * @kobj: object.
  639. *
  640. * The only thing special about this is that we remove any files in
  641. * the directory before we remove the directory, and we've inlined
  642. * what used to be sysfs_rmdir() below, instead of calling separately.
  643. */
  644. void sysfs_remove_dir(struct kobject * kobj)
  645. {
  646. struct sysfs_dirent *sd = kobj->sd;
  647. spin_lock(&sysfs_assoc_lock);
  648. kobj->sd = NULL;
  649. spin_unlock(&sysfs_assoc_lock);
  650. __sysfs_remove_dir(sd);
  651. }
  652. int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
  653. {
  654. struct sysfs_dirent *sd = kobj->sd;
  655. struct dentry *parent = NULL;
  656. struct dentry *old_dentry = NULL, *new_dentry = NULL;
  657. const char *dup_name = NULL;
  658. int error;
  659. mutex_lock(&sysfs_rename_mutex);
  660. error = 0;
  661. if (strcmp(sd->s_name, new_name) == 0)
  662. goto out; /* nothing to rename */
  663. /* get the original dentry */
  664. old_dentry = sysfs_get_dentry(sd);
  665. if (IS_ERR(old_dentry)) {
  666. error = PTR_ERR(old_dentry);
  667. goto out;
  668. }
  669. parent = old_dentry->d_parent;
  670. /* lock parent and get dentry for new name */
  671. mutex_lock(&parent->d_inode->i_mutex);
  672. mutex_lock(&sysfs_mutex);
  673. error = -EEXIST;
  674. if (sysfs_find_dirent(sd->s_parent, new_name))
  675. goto out_unlock;
  676. error = -ENOMEM;
  677. new_dentry = d_alloc_name(parent, new_name);
  678. if (!new_dentry)
  679. goto out_unlock;
  680. /* rename kobject and sysfs_dirent */
  681. error = -ENOMEM;
  682. new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
  683. if (!new_name)
  684. goto out_unlock;
  685. error = kobject_set_name(kobj, "%s", new_name);
  686. if (error)
  687. goto out_unlock;
  688. dup_name = sd->s_name;
  689. sd->s_name = new_name;
  690. /* rename */
  691. d_add(new_dentry, NULL);
  692. d_move(old_dentry, new_dentry);
  693. error = 0;
  694. out_unlock:
  695. mutex_unlock(&sysfs_mutex);
  696. mutex_unlock(&parent->d_inode->i_mutex);
  697. kfree(dup_name);
  698. dput(old_dentry);
  699. dput(new_dentry);
  700. out:
  701. mutex_unlock(&sysfs_rename_mutex);
  702. return error;
  703. }
  704. int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj)
  705. {
  706. struct sysfs_dirent *sd = kobj->sd;
  707. struct sysfs_dirent *new_parent_sd;
  708. struct dentry *old_parent, *new_parent = NULL;
  709. struct dentry *old_dentry = NULL, *new_dentry = NULL;
  710. int error;
  711. mutex_lock(&sysfs_rename_mutex);
  712. BUG_ON(!sd->s_parent);
  713. new_parent_sd = new_parent_kobj->sd ? new_parent_kobj->sd : &sysfs_root;
  714. error = 0;
  715. if (sd->s_parent == new_parent_sd)
  716. goto out; /* nothing to move */
  717. /* get dentries */
  718. old_dentry = sysfs_get_dentry(sd);
  719. if (IS_ERR(old_dentry)) {
  720. error = PTR_ERR(old_dentry);
  721. goto out;
  722. }
  723. old_parent = old_dentry->d_parent;
  724. new_parent = sysfs_get_dentry(new_parent_sd);
  725. if (IS_ERR(new_parent)) {
  726. error = PTR_ERR(new_parent);
  727. goto out;
  728. }
  729. again:
  730. mutex_lock(&old_parent->d_inode->i_mutex);
  731. if (!mutex_trylock(&new_parent->d_inode->i_mutex)) {
  732. mutex_unlock(&old_parent->d_inode->i_mutex);
  733. goto again;
  734. }
  735. mutex_lock(&sysfs_mutex);
  736. error = -EEXIST;
  737. if (sysfs_find_dirent(new_parent_sd, sd->s_name))
  738. goto out_unlock;
  739. error = -ENOMEM;
  740. new_dentry = d_alloc_name(new_parent, sd->s_name);
  741. if (!new_dentry)
  742. goto out_unlock;
  743. error = 0;
  744. d_add(new_dentry, NULL);
  745. d_move(old_dentry, new_dentry);
  746. dput(new_dentry);
  747. /* Remove from old parent's list and insert into new parent's list. */
  748. sysfs_unlink_sibling(sd);
  749. sysfs_get(new_parent_sd);
  750. sysfs_put(sd->s_parent);
  751. sd->s_parent = new_parent_sd;
  752. sysfs_link_sibling(sd);
  753. out_unlock:
  754. mutex_unlock(&sysfs_mutex);
  755. mutex_unlock(&new_parent->d_inode->i_mutex);
  756. mutex_unlock(&old_parent->d_inode->i_mutex);
  757. out:
  758. dput(new_parent);
  759. dput(old_dentry);
  760. dput(new_dentry);
  761. mutex_unlock(&sysfs_rename_mutex);
  762. return error;
  763. }
  764. /* Relationship between s_mode and the DT_xxx types */
  765. static inline unsigned char dt_type(struct sysfs_dirent *sd)
  766. {
  767. return (sd->s_mode >> 12) & 15;
  768. }
  769. static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
  770. {
  771. struct dentry *dentry = filp->f_path.dentry;
  772. struct sysfs_dirent * parent_sd = dentry->d_fsdata;
  773. struct sysfs_dirent *pos;
  774. ino_t ino;
  775. if (filp->f_pos == 0) {
  776. ino = parent_sd->s_ino;
  777. if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) == 0)
  778. filp->f_pos++;
  779. }
  780. if (filp->f_pos == 1) {
  781. if (parent_sd->s_parent)
  782. ino = parent_sd->s_parent->s_ino;
  783. else
  784. ino = parent_sd->s_ino;
  785. if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) == 0)
  786. filp->f_pos++;
  787. }
  788. if ((filp->f_pos > 1) && (filp->f_pos < INT_MAX)) {
  789. mutex_lock(&sysfs_mutex);
  790. /* Skip the dentries we have already reported */
  791. pos = parent_sd->s_children;
  792. while (pos && (filp->f_pos > pos->s_ino))
  793. pos = pos->s_sibling;
  794. for ( ; pos; pos = pos->s_sibling) {
  795. const char * name;
  796. int len;
  797. name = pos->s_name;
  798. len = strlen(name);
  799. filp->f_pos = ino = pos->s_ino;
  800. if (filldir(dirent, name, len, filp->f_pos, ino,
  801. dt_type(pos)) < 0)
  802. break;
  803. }
  804. if (!pos)
  805. filp->f_pos = INT_MAX;
  806. mutex_unlock(&sysfs_mutex);
  807. }
  808. return 0;
  809. }
  810. const struct file_operations sysfs_dir_operations = {
  811. .read = generic_read_dir,
  812. .readdir = sysfs_readdir,
  813. };