dir.c 23 KB

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