dir.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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. };
  656. static void remove_dir(struct sysfs_dirent *sd)
  657. {
  658. struct sysfs_addrm_cxt acxt;
  659. sysfs_addrm_start(&acxt, sd->s_parent);
  660. sysfs_remove_one(&acxt, sd);
  661. sysfs_addrm_finish(&acxt);
  662. }
  663. void sysfs_remove_subdir(struct sysfs_dirent *sd)
  664. {
  665. remove_dir(sd);
  666. }
  667. static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
  668. {
  669. struct sysfs_addrm_cxt acxt;
  670. struct sysfs_dirent **pos;
  671. if (!dir_sd)
  672. return;
  673. pr_debug("sysfs %s: removing dir\n", dir_sd->s_name);
  674. sysfs_addrm_start(&acxt, dir_sd);
  675. pos = &dir_sd->s_dir.children;
  676. while (*pos) {
  677. struct sysfs_dirent *sd = *pos;
  678. if (sysfs_type(sd) != SYSFS_DIR)
  679. sysfs_remove_one(&acxt, sd);
  680. else
  681. pos = &(*pos)->s_sibling;
  682. }
  683. sysfs_addrm_finish(&acxt);
  684. remove_dir(dir_sd);
  685. }
  686. /**
  687. * sysfs_remove_dir - remove an object's directory.
  688. * @kobj: object.
  689. *
  690. * The only thing special about this is that we remove any files in
  691. * the directory before we remove the directory, and we've inlined
  692. * what used to be sysfs_rmdir() below, instead of calling separately.
  693. */
  694. void sysfs_remove_dir(struct kobject * kobj)
  695. {
  696. struct sysfs_dirent *sd = kobj->sd;
  697. spin_lock(&sysfs_assoc_lock);
  698. kobj->sd = NULL;
  699. spin_unlock(&sysfs_assoc_lock);
  700. __sysfs_remove_dir(sd);
  701. }
  702. int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
  703. {
  704. struct sysfs_dirent *sd = kobj->sd;
  705. struct dentry *parent = NULL;
  706. struct dentry *old_dentry = NULL, *new_dentry = NULL;
  707. const char *dup_name = NULL;
  708. int error;
  709. mutex_lock(&sysfs_rename_mutex);
  710. error = 0;
  711. if (strcmp(sd->s_name, new_name) == 0)
  712. goto out; /* nothing to rename */
  713. /* get the original dentry */
  714. old_dentry = sysfs_get_dentry(sd);
  715. if (IS_ERR(old_dentry)) {
  716. error = PTR_ERR(old_dentry);
  717. old_dentry = NULL;
  718. goto out;
  719. }
  720. parent = old_dentry->d_parent;
  721. /* lock parent and get dentry for new name */
  722. mutex_lock(&parent->d_inode->i_mutex);
  723. mutex_lock(&sysfs_mutex);
  724. error = -EEXIST;
  725. if (sysfs_find_dirent(sd->s_parent, new_name))
  726. goto out_unlock;
  727. error = -ENOMEM;
  728. new_dentry = d_alloc_name(parent, new_name);
  729. if (!new_dentry)
  730. goto out_unlock;
  731. /* rename sysfs_dirent */
  732. error = -ENOMEM;
  733. new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
  734. if (!new_name)
  735. goto out_unlock;
  736. dup_name = sd->s_name;
  737. sd->s_name = new_name;
  738. /* rename */
  739. d_add(new_dentry, NULL);
  740. d_move(old_dentry, new_dentry);
  741. error = 0;
  742. out_unlock:
  743. mutex_unlock(&sysfs_mutex);
  744. mutex_unlock(&parent->d_inode->i_mutex);
  745. kfree(dup_name);
  746. dput(old_dentry);
  747. dput(new_dentry);
  748. out:
  749. mutex_unlock(&sysfs_rename_mutex);
  750. return error;
  751. }
  752. int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj)
  753. {
  754. struct sysfs_dirent *sd = kobj->sd;
  755. struct sysfs_dirent *new_parent_sd;
  756. struct dentry *old_parent, *new_parent = NULL;
  757. struct dentry *old_dentry = NULL, *new_dentry = NULL;
  758. int error;
  759. mutex_lock(&sysfs_rename_mutex);
  760. BUG_ON(!sd->s_parent);
  761. new_parent_sd = new_parent_kobj->sd ? new_parent_kobj->sd : &sysfs_root;
  762. error = 0;
  763. if (sd->s_parent == new_parent_sd)
  764. goto out; /* nothing to move */
  765. /* get dentries */
  766. old_dentry = sysfs_get_dentry(sd);
  767. if (IS_ERR(old_dentry)) {
  768. error = PTR_ERR(old_dentry);
  769. old_dentry = NULL;
  770. goto out;
  771. }
  772. old_parent = old_dentry->d_parent;
  773. new_parent = sysfs_get_dentry(new_parent_sd);
  774. if (IS_ERR(new_parent)) {
  775. error = PTR_ERR(new_parent);
  776. new_parent = NULL;
  777. goto out;
  778. }
  779. again:
  780. mutex_lock(&old_parent->d_inode->i_mutex);
  781. if (!mutex_trylock(&new_parent->d_inode->i_mutex)) {
  782. mutex_unlock(&old_parent->d_inode->i_mutex);
  783. goto again;
  784. }
  785. mutex_lock(&sysfs_mutex);
  786. error = -EEXIST;
  787. if (sysfs_find_dirent(new_parent_sd, sd->s_name))
  788. goto out_unlock;
  789. error = -ENOMEM;
  790. new_dentry = d_alloc_name(new_parent, sd->s_name);
  791. if (!new_dentry)
  792. goto out_unlock;
  793. error = 0;
  794. d_add(new_dentry, NULL);
  795. d_move(old_dentry, new_dentry);
  796. /* Remove from old parent's list and insert into new parent's list. */
  797. sysfs_unlink_sibling(sd);
  798. sysfs_get(new_parent_sd);
  799. sysfs_put(sd->s_parent);
  800. sd->s_parent = new_parent_sd;
  801. sysfs_link_sibling(sd);
  802. out_unlock:
  803. mutex_unlock(&sysfs_mutex);
  804. mutex_unlock(&new_parent->d_inode->i_mutex);
  805. mutex_unlock(&old_parent->d_inode->i_mutex);
  806. out:
  807. dput(new_parent);
  808. dput(old_dentry);
  809. dput(new_dentry);
  810. mutex_unlock(&sysfs_rename_mutex);
  811. return error;
  812. }
  813. /* Relationship between s_mode and the DT_xxx types */
  814. static inline unsigned char dt_type(struct sysfs_dirent *sd)
  815. {
  816. return (sd->s_mode >> 12) & 15;
  817. }
  818. static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
  819. {
  820. struct dentry *dentry = filp->f_path.dentry;
  821. struct sysfs_dirent * parent_sd = dentry->d_fsdata;
  822. struct sysfs_dirent *pos;
  823. ino_t ino;
  824. if (filp->f_pos == 0) {
  825. ino = parent_sd->s_ino;
  826. if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) == 0)
  827. filp->f_pos++;
  828. }
  829. if (filp->f_pos == 1) {
  830. if (parent_sd->s_parent)
  831. ino = parent_sd->s_parent->s_ino;
  832. else
  833. ino = parent_sd->s_ino;
  834. if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) == 0)
  835. filp->f_pos++;
  836. }
  837. if ((filp->f_pos > 1) && (filp->f_pos < INT_MAX)) {
  838. mutex_lock(&sysfs_mutex);
  839. /* Skip the dentries we have already reported */
  840. pos = parent_sd->s_dir.children;
  841. while (pos && (filp->f_pos > pos->s_ino))
  842. pos = pos->s_sibling;
  843. for ( ; pos; pos = pos->s_sibling) {
  844. const char * name;
  845. int len;
  846. name = pos->s_name;
  847. len = strlen(name);
  848. filp->f_pos = ino = pos->s_ino;
  849. if (filldir(dirent, name, len, filp->f_pos, ino,
  850. dt_type(pos)) < 0)
  851. break;
  852. }
  853. if (!pos)
  854. filp->f_pos = INT_MAX;
  855. mutex_unlock(&sysfs_mutex);
  856. }
  857. return 0;
  858. }
  859. const struct file_operations sysfs_dir_operations = {
  860. .read = generic_read_dir,
  861. .readdir = sysfs_readdir,
  862. .llseek = generic_file_llseek,
  863. };