dir.c 22 KB

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