dir.c 21 KB

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