dir.c 21 KB

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