dir.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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 <linux/security.h>
  23. #include "sysfs.h"
  24. DEFINE_MUTEX(sysfs_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 rb_node **p;
  42. struct rb_node *parent;
  43. if (sysfs_type(sd) == SYSFS_DIR)
  44. parent_sd->s_dir.subdirs++;
  45. p = &parent_sd->s_dir.inode_tree.rb_node;
  46. parent = NULL;
  47. while (*p) {
  48. parent = *p;
  49. #define node rb_entry(parent, struct sysfs_dirent, inode_node)
  50. if (sd->s_ino < node->s_ino) {
  51. p = &node->inode_node.rb_left;
  52. } else if (sd->s_ino > node->s_ino) {
  53. p = &node->inode_node.rb_right;
  54. } else {
  55. printk(KERN_CRIT "sysfs: inserting duplicate inode '%lx'\n",
  56. (unsigned long) sd->s_ino);
  57. BUG();
  58. }
  59. #undef node
  60. }
  61. rb_link_node(&sd->inode_node, parent, p);
  62. rb_insert_color(&sd->inode_node, &parent_sd->s_dir.inode_tree);
  63. p = &parent_sd->s_dir.name_tree.rb_node;
  64. parent = NULL;
  65. while (*p) {
  66. int c;
  67. parent = *p;
  68. #define node rb_entry(parent, struct sysfs_dirent, name_node)
  69. c = strcmp(sd->s_name, node->s_name);
  70. if (c < 0) {
  71. p = &node->name_node.rb_left;
  72. } else {
  73. p = &node->name_node.rb_right;
  74. }
  75. #undef node
  76. }
  77. rb_link_node(&sd->name_node, parent, p);
  78. rb_insert_color(&sd->name_node, &parent_sd->s_dir.name_tree);
  79. }
  80. /**
  81. * sysfs_unlink_sibling - unlink sysfs_dirent from sibling list
  82. * @sd: sysfs_dirent of interest
  83. *
  84. * Unlink @sd from its sibling list which starts from
  85. * sd->s_parent->s_dir.children.
  86. *
  87. * Locking:
  88. * mutex_lock(sysfs_mutex)
  89. */
  90. static void sysfs_unlink_sibling(struct sysfs_dirent *sd)
  91. {
  92. if (sysfs_type(sd) == SYSFS_DIR)
  93. sd->s_parent->s_dir.subdirs--;
  94. rb_erase(&sd->inode_node, &sd->s_parent->s_dir.inode_tree);
  95. rb_erase(&sd->name_node, &sd->s_parent->s_dir.name_tree);
  96. }
  97. /**
  98. * sysfs_get_active - get an active reference to sysfs_dirent
  99. * @sd: sysfs_dirent to get an active reference to
  100. *
  101. * Get an active reference of @sd. This function is noop if @sd
  102. * is NULL.
  103. *
  104. * RETURNS:
  105. * Pointer to @sd on success, NULL on failure.
  106. */
  107. struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
  108. {
  109. if (unlikely(!sd))
  110. return NULL;
  111. while (1) {
  112. int v, t;
  113. v = atomic_read(&sd->s_active);
  114. if (unlikely(v < 0))
  115. return NULL;
  116. t = atomic_cmpxchg(&sd->s_active, v, v + 1);
  117. if (likely(t == v)) {
  118. rwsem_acquire_read(&sd->dep_map, 0, 1, _RET_IP_);
  119. return sd;
  120. }
  121. if (t < 0)
  122. return NULL;
  123. cpu_relax();
  124. }
  125. }
  126. /**
  127. * sysfs_put_active - put an active reference to sysfs_dirent
  128. * @sd: sysfs_dirent to put an active reference to
  129. *
  130. * Put an active reference to @sd. This function is noop if @sd
  131. * is NULL.
  132. */
  133. void sysfs_put_active(struct sysfs_dirent *sd)
  134. {
  135. int v;
  136. if (unlikely(!sd))
  137. return;
  138. rwsem_release(&sd->dep_map, 1, _RET_IP_);
  139. v = atomic_dec_return(&sd->s_active);
  140. if (likely(v != SD_DEACTIVATED_BIAS))
  141. return;
  142. /* atomic_dec_return() is a mb(), we'll always see the updated
  143. * sd->u.completion.
  144. */
  145. complete(sd->u.completion);
  146. }
  147. /**
  148. * sysfs_deactivate - deactivate sysfs_dirent
  149. * @sd: sysfs_dirent to deactivate
  150. *
  151. * Deny new active references and drain existing ones.
  152. */
  153. static void sysfs_deactivate(struct sysfs_dirent *sd)
  154. {
  155. DECLARE_COMPLETION_ONSTACK(wait);
  156. int v;
  157. BUG_ON(!(sd->s_flags & SYSFS_FLAG_REMOVED));
  158. if (!(sysfs_type(sd) & SYSFS_ACTIVE_REF))
  159. return;
  160. sd->u.completion = (void *)&wait;
  161. rwsem_acquire(&sd->dep_map, 0, 0, _RET_IP_);
  162. /* atomic_add_return() is a mb(), put_active() will always see
  163. * the updated sd->u.completion.
  164. */
  165. v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
  166. if (v != SD_DEACTIVATED_BIAS) {
  167. lock_contended(&sd->dep_map, _RET_IP_);
  168. wait_for_completion(&wait);
  169. }
  170. lock_acquired(&sd->dep_map, _RET_IP_);
  171. rwsem_release(&sd->dep_map, 1, _RET_IP_);
  172. }
  173. static int sysfs_alloc_ino(ino_t *pino)
  174. {
  175. int ino, rc;
  176. retry:
  177. spin_lock(&sysfs_ino_lock);
  178. rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
  179. spin_unlock(&sysfs_ino_lock);
  180. if (rc == -EAGAIN) {
  181. if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
  182. goto retry;
  183. rc = -ENOMEM;
  184. }
  185. *pino = ino;
  186. return rc;
  187. }
  188. static void sysfs_free_ino(ino_t ino)
  189. {
  190. spin_lock(&sysfs_ino_lock);
  191. ida_remove(&sysfs_ino_ida, ino);
  192. spin_unlock(&sysfs_ino_lock);
  193. }
  194. void release_sysfs_dirent(struct sysfs_dirent * sd)
  195. {
  196. struct sysfs_dirent *parent_sd;
  197. repeat:
  198. /* Moving/renaming is always done while holding reference.
  199. * sd->s_parent won't change beneath us.
  200. */
  201. parent_sd = sd->s_parent;
  202. if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
  203. sysfs_put(sd->s_symlink.target_sd);
  204. if (sysfs_type(sd) & SYSFS_COPY_NAME)
  205. kfree(sd->s_name);
  206. if (sd->s_iattr && sd->s_iattr->ia_secdata)
  207. security_release_secctx(sd->s_iattr->ia_secdata,
  208. sd->s_iattr->ia_secdata_len);
  209. kfree(sd->s_iattr);
  210. sysfs_free_ino(sd->s_ino);
  211. kmem_cache_free(sysfs_dir_cachep, sd);
  212. sd = parent_sd;
  213. if (sd && atomic_dec_and_test(&sd->s_count))
  214. goto repeat;
  215. }
  216. static int sysfs_dentry_delete(const struct dentry *dentry)
  217. {
  218. struct sysfs_dirent *sd = dentry->d_fsdata;
  219. return !!(sd->s_flags & SYSFS_FLAG_REMOVED);
  220. }
  221. static int sysfs_dentry_revalidate(struct dentry *dentry, struct nameidata *nd)
  222. {
  223. struct sysfs_dirent *sd;
  224. int is_dir;
  225. if (nd->flags & LOOKUP_RCU)
  226. return -ECHILD;
  227. sd = dentry->d_fsdata;
  228. mutex_lock(&sysfs_mutex);
  229. /* The sysfs dirent has been deleted */
  230. if (sd->s_flags & SYSFS_FLAG_REMOVED)
  231. goto out_bad;
  232. /* The sysfs dirent has been moved? */
  233. if (dentry->d_parent->d_fsdata != sd->s_parent)
  234. goto out_bad;
  235. /* The sysfs dirent has been renamed */
  236. if (strcmp(dentry->d_name.name, sd->s_name) != 0)
  237. goto out_bad;
  238. mutex_unlock(&sysfs_mutex);
  239. out_valid:
  240. return 1;
  241. out_bad:
  242. /* Remove the dentry from the dcache hashes.
  243. * If this is a deleted dentry we use d_drop instead of d_delete
  244. * so sysfs doesn't need to cope with negative dentries.
  245. *
  246. * If this is a dentry that has simply been renamed we
  247. * use d_drop to remove it from the dcache lookup on its
  248. * old parent. If this dentry persists later when a lookup
  249. * is performed at its new name the dentry will be readded
  250. * to the dcache hashes.
  251. */
  252. is_dir = (sysfs_type(sd) == SYSFS_DIR);
  253. mutex_unlock(&sysfs_mutex);
  254. if (is_dir) {
  255. /* If we have submounts we must allow the vfs caches
  256. * to lie about the state of the filesystem to prevent
  257. * leaks and other nasty things.
  258. */
  259. if (have_submounts(dentry))
  260. goto out_valid;
  261. shrink_dcache_parent(dentry);
  262. }
  263. d_drop(dentry);
  264. return 0;
  265. }
  266. static void sysfs_dentry_iput(struct dentry *dentry, struct inode *inode)
  267. {
  268. struct sysfs_dirent * sd = dentry->d_fsdata;
  269. sysfs_put(sd);
  270. iput(inode);
  271. }
  272. static const struct dentry_operations sysfs_dentry_ops = {
  273. .d_revalidate = sysfs_dentry_revalidate,
  274. .d_delete = sysfs_dentry_delete,
  275. .d_iput = sysfs_dentry_iput,
  276. };
  277. struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
  278. {
  279. char *dup_name = NULL;
  280. struct sysfs_dirent *sd;
  281. if (type & SYSFS_COPY_NAME) {
  282. name = dup_name = kstrdup(name, GFP_KERNEL);
  283. if (!name)
  284. return NULL;
  285. }
  286. sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
  287. if (!sd)
  288. goto err_out1;
  289. if (sysfs_alloc_ino(&sd->s_ino))
  290. goto err_out2;
  291. atomic_set(&sd->s_count, 1);
  292. atomic_set(&sd->s_active, 0);
  293. sd->s_name = name;
  294. sd->s_mode = mode;
  295. sd->s_flags = type;
  296. return sd;
  297. err_out2:
  298. kmem_cache_free(sysfs_dir_cachep, sd);
  299. err_out1:
  300. kfree(dup_name);
  301. return NULL;
  302. }
  303. /**
  304. * sysfs_addrm_start - prepare for sysfs_dirent add/remove
  305. * @acxt: pointer to sysfs_addrm_cxt to be used
  306. * @parent_sd: parent sysfs_dirent
  307. *
  308. * This function is called when the caller is about to add or
  309. * remove sysfs_dirent under @parent_sd. This function acquires
  310. * sysfs_mutex. @acxt is used to keep and pass context to
  311. * other addrm functions.
  312. *
  313. * LOCKING:
  314. * Kernel thread context (may sleep). sysfs_mutex is locked on
  315. * return.
  316. */
  317. void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt,
  318. struct sysfs_dirent *parent_sd)
  319. {
  320. memset(acxt, 0, sizeof(*acxt));
  321. acxt->parent_sd = parent_sd;
  322. mutex_lock(&sysfs_mutex);
  323. }
  324. /**
  325. * __sysfs_add_one - add sysfs_dirent to parent without warning
  326. * @acxt: addrm context to use
  327. * @sd: sysfs_dirent to be added
  328. *
  329. * Get @acxt->parent_sd and set sd->s_parent to it and increment
  330. * nlink of parent inode if @sd is a directory and link into the
  331. * children list of the parent.
  332. *
  333. * This function should be called between calls to
  334. * sysfs_addrm_start() and sysfs_addrm_finish() and should be
  335. * passed the same @acxt as passed to sysfs_addrm_start().
  336. *
  337. * LOCKING:
  338. * Determined by sysfs_addrm_start().
  339. *
  340. * RETURNS:
  341. * 0 on success, -EEXIST if entry with the given name already
  342. * exists.
  343. */
  344. int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
  345. {
  346. struct sysfs_inode_attrs *ps_iattr;
  347. if (!!sysfs_ns_type(acxt->parent_sd) != !!sd->s_ns) {
  348. WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n",
  349. sysfs_ns_type(acxt->parent_sd)? "required": "invalid",
  350. acxt->parent_sd->s_name, sd->s_name);
  351. return -EINVAL;
  352. }
  353. if (sysfs_find_dirent(acxt->parent_sd, sd->s_ns, sd->s_name))
  354. return -EEXIST;
  355. sd->s_parent = sysfs_get(acxt->parent_sd);
  356. sysfs_link_sibling(sd);
  357. /* Update timestamps on the parent */
  358. ps_iattr = acxt->parent_sd->s_iattr;
  359. if (ps_iattr) {
  360. struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
  361. ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
  362. }
  363. return 0;
  364. }
  365. /**
  366. * sysfs_pathname - return full path to sysfs dirent
  367. * @sd: sysfs_dirent whose path we want
  368. * @path: caller allocated buffer
  369. *
  370. * Gives the name "/" to the sysfs_root entry; any path returned
  371. * is relative to wherever sysfs is mounted.
  372. *
  373. * XXX: does no error checking on @path size
  374. */
  375. static char *sysfs_pathname(struct sysfs_dirent *sd, char *path)
  376. {
  377. if (sd->s_parent) {
  378. sysfs_pathname(sd->s_parent, path);
  379. strcat(path, "/");
  380. }
  381. strcat(path, sd->s_name);
  382. return path;
  383. }
  384. /**
  385. * sysfs_add_one - add sysfs_dirent to parent
  386. * @acxt: addrm context to use
  387. * @sd: sysfs_dirent to be added
  388. *
  389. * Get @acxt->parent_sd and set sd->s_parent to it and increment
  390. * nlink of parent inode if @sd is a directory and link into the
  391. * children list of the parent.
  392. *
  393. * This function should be called between calls to
  394. * sysfs_addrm_start() and sysfs_addrm_finish() and should be
  395. * passed the same @acxt as passed to sysfs_addrm_start().
  396. *
  397. * LOCKING:
  398. * Determined by sysfs_addrm_start().
  399. *
  400. * RETURNS:
  401. * 0 on success, -EEXIST if entry with the given name already
  402. * exists.
  403. */
  404. int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
  405. {
  406. int ret;
  407. ret = __sysfs_add_one(acxt, sd);
  408. if (ret == -EEXIST) {
  409. char *path = kzalloc(PATH_MAX, GFP_KERNEL);
  410. WARN(1, KERN_WARNING
  411. "sysfs: cannot create duplicate filename '%s'\n",
  412. (path == NULL) ? sd->s_name :
  413. strcat(strcat(sysfs_pathname(acxt->parent_sd, path), "/"),
  414. sd->s_name));
  415. kfree(path);
  416. }
  417. return ret;
  418. }
  419. /**
  420. * sysfs_remove_one - remove sysfs_dirent from parent
  421. * @acxt: addrm context to use
  422. * @sd: sysfs_dirent to be removed
  423. *
  424. * Mark @sd removed and drop nlink of parent inode if @sd is a
  425. * directory. @sd is unlinked from the children list.
  426. *
  427. * This function should be called between calls to
  428. * sysfs_addrm_start() and sysfs_addrm_finish() and should be
  429. * passed the same @acxt as passed to sysfs_addrm_start().
  430. *
  431. * LOCKING:
  432. * Determined by sysfs_addrm_start().
  433. */
  434. void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
  435. {
  436. struct sysfs_inode_attrs *ps_iattr;
  437. BUG_ON(sd->s_flags & SYSFS_FLAG_REMOVED);
  438. sysfs_unlink_sibling(sd);
  439. /* Update timestamps on the parent */
  440. ps_iattr = acxt->parent_sd->s_iattr;
  441. if (ps_iattr) {
  442. struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
  443. ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
  444. }
  445. sd->s_flags |= SYSFS_FLAG_REMOVED;
  446. sd->u.removed_list = acxt->removed;
  447. acxt->removed = sd;
  448. }
  449. /**
  450. * sysfs_addrm_finish - finish up sysfs_dirent add/remove
  451. * @acxt: addrm context to finish up
  452. *
  453. * Finish up sysfs_dirent add/remove. Resources acquired by
  454. * sysfs_addrm_start() are released and removed sysfs_dirents are
  455. * cleaned up.
  456. *
  457. * LOCKING:
  458. * sysfs_mutex is released.
  459. */
  460. void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
  461. {
  462. /* release resources acquired by sysfs_addrm_start() */
  463. mutex_unlock(&sysfs_mutex);
  464. /* kill removed sysfs_dirents */
  465. while (acxt->removed) {
  466. struct sysfs_dirent *sd = acxt->removed;
  467. acxt->removed = sd->u.removed_list;
  468. sysfs_deactivate(sd);
  469. unmap_bin_file(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 void *ns,
  488. const unsigned char *name)
  489. {
  490. struct rb_node *p = parent_sd->s_dir.name_tree.rb_node;
  491. struct sysfs_dirent *found = NULL;
  492. if (!!sysfs_ns_type(parent_sd) != !!ns) {
  493. WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n",
  494. sysfs_ns_type(parent_sd)? "required": "invalid",
  495. parent_sd->s_name, name);
  496. return NULL;
  497. }
  498. while (p) {
  499. int c;
  500. #define node rb_entry(p, struct sysfs_dirent, name_node)
  501. c = strcmp(name, node->s_name);
  502. if (c < 0) {
  503. p = node->name_node.rb_left;
  504. } else if (c > 0) {
  505. p = node->name_node.rb_right;
  506. } else {
  507. found = node;
  508. p = node->name_node.rb_left;
  509. }
  510. #undef node
  511. }
  512. if (found) {
  513. while (found->s_ns != ns) {
  514. p = rb_next(&found->name_node);
  515. if (!p)
  516. return NULL;
  517. found = rb_entry(p, struct sysfs_dirent, name_node);
  518. if (strcmp(name, found->s_name))
  519. return NULL;
  520. }
  521. }
  522. return found;
  523. }
  524. /**
  525. * sysfs_get_dirent - find and get sysfs_dirent with the given name
  526. * @parent_sd: sysfs_dirent to search under
  527. * @name: name to look for
  528. *
  529. * Look for sysfs_dirent with name @name under @parent_sd and get
  530. * it if found.
  531. *
  532. * LOCKING:
  533. * Kernel thread context (may sleep). Grabs sysfs_mutex.
  534. *
  535. * RETURNS:
  536. * Pointer to sysfs_dirent if found, NULL if not.
  537. */
  538. struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
  539. const void *ns,
  540. const unsigned char *name)
  541. {
  542. struct sysfs_dirent *sd;
  543. mutex_lock(&sysfs_mutex);
  544. sd = sysfs_find_dirent(parent_sd, ns, name);
  545. sysfs_get(sd);
  546. mutex_unlock(&sysfs_mutex);
  547. return sd;
  548. }
  549. EXPORT_SYMBOL_GPL(sysfs_get_dirent);
  550. static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
  551. enum kobj_ns_type type, const void *ns, const char *name,
  552. struct sysfs_dirent **p_sd)
  553. {
  554. umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
  555. struct sysfs_addrm_cxt acxt;
  556. struct sysfs_dirent *sd;
  557. int rc;
  558. /* allocate */
  559. sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
  560. if (!sd)
  561. return -ENOMEM;
  562. sd->s_flags |= (type << SYSFS_NS_TYPE_SHIFT);
  563. sd->s_ns = ns;
  564. sd->s_dir.kobj = kobj;
  565. /* link in */
  566. sysfs_addrm_start(&acxt, parent_sd);
  567. rc = sysfs_add_one(&acxt, sd);
  568. sysfs_addrm_finish(&acxt);
  569. if (rc == 0)
  570. *p_sd = sd;
  571. else
  572. sysfs_put(sd);
  573. return rc;
  574. }
  575. int sysfs_create_subdir(struct kobject *kobj, const char *name,
  576. struct sysfs_dirent **p_sd)
  577. {
  578. return create_dir(kobj, kobj->sd,
  579. KOBJ_NS_TYPE_NONE, NULL, name, p_sd);
  580. }
  581. /**
  582. * sysfs_read_ns_type: return associated ns_type
  583. * @kobj: the kobject being queried
  584. *
  585. * Each kobject can be tagged with exactly one namespace type
  586. * (i.e. network or user). Return the ns_type associated with
  587. * this object if any
  588. */
  589. static enum kobj_ns_type sysfs_read_ns_type(struct kobject *kobj)
  590. {
  591. const struct kobj_ns_type_operations *ops;
  592. enum kobj_ns_type type;
  593. ops = kobj_child_ns_ops(kobj);
  594. if (!ops)
  595. return KOBJ_NS_TYPE_NONE;
  596. type = ops->type;
  597. BUG_ON(type <= KOBJ_NS_TYPE_NONE);
  598. BUG_ON(type >= KOBJ_NS_TYPES);
  599. BUG_ON(!kobj_ns_type_registered(type));
  600. return type;
  601. }
  602. /**
  603. * sysfs_create_dir - create a directory for an object.
  604. * @kobj: object we're creating directory for.
  605. */
  606. int sysfs_create_dir(struct kobject * kobj)
  607. {
  608. enum kobj_ns_type type;
  609. struct sysfs_dirent *parent_sd, *sd;
  610. const void *ns = NULL;
  611. int error = 0;
  612. BUG_ON(!kobj);
  613. if (kobj->parent)
  614. parent_sd = kobj->parent->sd;
  615. else
  616. parent_sd = &sysfs_root;
  617. if (sysfs_ns_type(parent_sd))
  618. ns = kobj->ktype->namespace(kobj);
  619. type = sysfs_read_ns_type(kobj);
  620. error = create_dir(kobj, parent_sd, type, ns, kobject_name(kobj), &sd);
  621. if (!error)
  622. kobj->sd = sd;
  623. return error;
  624. }
  625. static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
  626. struct nameidata *nd)
  627. {
  628. struct dentry *ret = NULL;
  629. struct dentry *parent = dentry->d_parent;
  630. struct sysfs_dirent *parent_sd = parent->d_fsdata;
  631. struct sysfs_dirent *sd;
  632. struct inode *inode;
  633. enum kobj_ns_type type;
  634. const void *ns;
  635. mutex_lock(&sysfs_mutex);
  636. type = sysfs_ns_type(parent_sd);
  637. ns = sysfs_info(dir->i_sb)->ns[type];
  638. sd = sysfs_find_dirent(parent_sd, ns, dentry->d_name.name);
  639. /* no such entry */
  640. if (!sd) {
  641. ret = ERR_PTR(-ENOENT);
  642. goto out_unlock;
  643. }
  644. /* attach dentry and inode */
  645. inode = sysfs_get_inode(dir->i_sb, sd);
  646. if (!inode) {
  647. ret = ERR_PTR(-ENOMEM);
  648. goto out_unlock;
  649. }
  650. /* instantiate and hash dentry */
  651. ret = d_find_alias(inode);
  652. if (!ret) {
  653. d_set_d_op(dentry, &sysfs_dentry_ops);
  654. dentry->d_fsdata = sysfs_get(sd);
  655. d_add(dentry, inode);
  656. } else {
  657. d_move(ret, dentry);
  658. iput(inode);
  659. }
  660. out_unlock:
  661. mutex_unlock(&sysfs_mutex);
  662. return ret;
  663. }
  664. const struct inode_operations sysfs_dir_inode_operations = {
  665. .lookup = sysfs_lookup,
  666. .permission = sysfs_permission,
  667. .setattr = sysfs_setattr,
  668. .getattr = sysfs_getattr,
  669. .setxattr = sysfs_setxattr,
  670. };
  671. static void remove_dir(struct sysfs_dirent *sd)
  672. {
  673. struct sysfs_addrm_cxt acxt;
  674. sysfs_addrm_start(&acxt, sd->s_parent);
  675. sysfs_remove_one(&acxt, sd);
  676. sysfs_addrm_finish(&acxt);
  677. }
  678. void sysfs_remove_subdir(struct sysfs_dirent *sd)
  679. {
  680. remove_dir(sd);
  681. }
  682. static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
  683. {
  684. struct sysfs_addrm_cxt acxt;
  685. struct rb_node *pos;
  686. if (!dir_sd)
  687. return;
  688. pr_debug("sysfs %s: removing dir\n", dir_sd->s_name);
  689. sysfs_addrm_start(&acxt, dir_sd);
  690. pos = rb_first(&dir_sd->s_dir.inode_tree);
  691. while (pos) {
  692. struct sysfs_dirent *sd = rb_entry(pos, struct sysfs_dirent, inode_node);
  693. pos = rb_next(pos);
  694. if (sysfs_type(sd) != SYSFS_DIR)
  695. sysfs_remove_one(&acxt, sd);
  696. }
  697. sysfs_addrm_finish(&acxt);
  698. remove_dir(dir_sd);
  699. }
  700. /**
  701. * sysfs_remove_dir - remove an object's directory.
  702. * @kobj: object.
  703. *
  704. * The only thing special about this is that we remove any files in
  705. * the directory before we remove the directory, and we've inlined
  706. * what used to be sysfs_rmdir() below, instead of calling separately.
  707. */
  708. void sysfs_remove_dir(struct kobject * kobj)
  709. {
  710. struct sysfs_dirent *sd = kobj->sd;
  711. spin_lock(&sysfs_assoc_lock);
  712. kobj->sd = NULL;
  713. spin_unlock(&sysfs_assoc_lock);
  714. __sysfs_remove_dir(sd);
  715. }
  716. int sysfs_rename(struct sysfs_dirent *sd,
  717. struct sysfs_dirent *new_parent_sd, const void *new_ns,
  718. const char *new_name)
  719. {
  720. const char *dup_name = NULL;
  721. int error;
  722. mutex_lock(&sysfs_mutex);
  723. error = 0;
  724. if ((sd->s_parent == new_parent_sd) && (sd->s_ns == new_ns) &&
  725. (strcmp(sd->s_name, new_name) == 0))
  726. goto out; /* nothing to rename */
  727. error = -EEXIST;
  728. if (sysfs_find_dirent(new_parent_sd, new_ns, new_name))
  729. goto out;
  730. /* rename sysfs_dirent */
  731. if (strcmp(sd->s_name, new_name) != 0) {
  732. error = -ENOMEM;
  733. new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
  734. if (!new_name)
  735. goto out;
  736. dup_name = sd->s_name;
  737. sd->s_name = new_name;
  738. }
  739. /* Remove from old parent's list and insert into new parent's list. */
  740. if (sd->s_parent != new_parent_sd) {
  741. sysfs_unlink_sibling(sd);
  742. sysfs_get(new_parent_sd);
  743. sysfs_put(sd->s_parent);
  744. sd->s_parent = new_parent_sd;
  745. sysfs_link_sibling(sd);
  746. }
  747. sd->s_ns = new_ns;
  748. error = 0;
  749. out:
  750. mutex_unlock(&sysfs_mutex);
  751. kfree(dup_name);
  752. return error;
  753. }
  754. int sysfs_rename_dir(struct kobject *kobj, const char *new_name)
  755. {
  756. struct sysfs_dirent *parent_sd = kobj->sd->s_parent;
  757. const void *new_ns = NULL;
  758. if (sysfs_ns_type(parent_sd))
  759. new_ns = kobj->ktype->namespace(kobj);
  760. return sysfs_rename(kobj->sd, parent_sd, new_ns, new_name);
  761. }
  762. int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj)
  763. {
  764. struct sysfs_dirent *sd = kobj->sd;
  765. struct sysfs_dirent *new_parent_sd;
  766. const void *new_ns = NULL;
  767. BUG_ON(!sd->s_parent);
  768. if (sysfs_ns_type(sd->s_parent))
  769. new_ns = kobj->ktype->namespace(kobj);
  770. new_parent_sd = new_parent_kobj && new_parent_kobj->sd ?
  771. new_parent_kobj->sd : &sysfs_root;
  772. return sysfs_rename(sd, new_parent_sd, new_ns, sd->s_name);
  773. }
  774. /* Relationship between s_mode and the DT_xxx types */
  775. static inline unsigned char dt_type(struct sysfs_dirent *sd)
  776. {
  777. return (sd->s_mode >> 12) & 15;
  778. }
  779. static int sysfs_dir_release(struct inode *inode, struct file *filp)
  780. {
  781. sysfs_put(filp->private_data);
  782. return 0;
  783. }
  784. static struct sysfs_dirent *sysfs_dir_pos(const void *ns,
  785. struct sysfs_dirent *parent_sd, ino_t ino, struct sysfs_dirent *pos)
  786. {
  787. if (pos) {
  788. int valid = !(pos->s_flags & SYSFS_FLAG_REMOVED) &&
  789. pos->s_parent == parent_sd &&
  790. ino == pos->s_ino;
  791. sysfs_put(pos);
  792. if (!valid)
  793. pos = NULL;
  794. }
  795. if (!pos && (ino > 1) && (ino < INT_MAX)) {
  796. struct rb_node *p = parent_sd->s_dir.inode_tree.rb_node;
  797. while (p) {
  798. #define node rb_entry(p, struct sysfs_dirent, inode_node)
  799. if (ino < node->s_ino) {
  800. pos = node;
  801. p = node->inode_node.rb_left;
  802. } else if (ino > node->s_ino) {
  803. p = node->inode_node.rb_right;
  804. } else {
  805. pos = node;
  806. break;
  807. }
  808. #undef node
  809. }
  810. }
  811. while (pos && pos->s_ns != ns) {
  812. struct rb_node *p = rb_next(&pos->inode_node);
  813. if (!p)
  814. pos = NULL;
  815. else
  816. pos = rb_entry(p, struct sysfs_dirent, inode_node);
  817. }
  818. return pos;
  819. }
  820. static struct sysfs_dirent *sysfs_dir_next_pos(const void *ns,
  821. struct sysfs_dirent *parent_sd, ino_t ino, struct sysfs_dirent *pos)
  822. {
  823. pos = sysfs_dir_pos(ns, parent_sd, ino, pos);
  824. if (pos) do {
  825. struct rb_node *p = rb_next(&pos->inode_node);
  826. if (!p)
  827. pos = NULL;
  828. else
  829. pos = rb_entry(p, struct sysfs_dirent, inode_node);
  830. } while (pos && pos->s_ns != ns);
  831. return pos;
  832. }
  833. static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
  834. {
  835. struct dentry *dentry = filp->f_path.dentry;
  836. struct sysfs_dirent * parent_sd = dentry->d_fsdata;
  837. struct sysfs_dirent *pos = filp->private_data;
  838. enum kobj_ns_type type;
  839. const void *ns;
  840. ino_t ino;
  841. type = sysfs_ns_type(parent_sd);
  842. ns = sysfs_info(dentry->d_sb)->ns[type];
  843. if (filp->f_pos == 0) {
  844. ino = parent_sd->s_ino;
  845. if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) == 0)
  846. filp->f_pos++;
  847. }
  848. if (filp->f_pos == 1) {
  849. if (parent_sd->s_parent)
  850. ino = parent_sd->s_parent->s_ino;
  851. else
  852. ino = parent_sd->s_ino;
  853. if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) == 0)
  854. filp->f_pos++;
  855. }
  856. mutex_lock(&sysfs_mutex);
  857. for (pos = sysfs_dir_pos(ns, parent_sd, filp->f_pos, pos);
  858. pos;
  859. pos = sysfs_dir_next_pos(ns, parent_sd, filp->f_pos, pos)) {
  860. const char * name;
  861. unsigned int type;
  862. int len, ret;
  863. name = pos->s_name;
  864. len = strlen(name);
  865. ino = pos->s_ino;
  866. type = dt_type(pos);
  867. filp->f_pos = ino;
  868. filp->private_data = sysfs_get(pos);
  869. mutex_unlock(&sysfs_mutex);
  870. ret = filldir(dirent, name, len, filp->f_pos, ino, type);
  871. mutex_lock(&sysfs_mutex);
  872. if (ret < 0)
  873. break;
  874. }
  875. mutex_unlock(&sysfs_mutex);
  876. if ((filp->f_pos > 1) && !pos) { /* EOF */
  877. filp->f_pos = INT_MAX;
  878. filp->private_data = NULL;
  879. }
  880. return 0;
  881. }
  882. const struct file_operations sysfs_dir_operations = {
  883. .read = generic_read_dir,
  884. .readdir = sysfs_readdir,
  885. .release = sysfs_dir_release,
  886. .llseek = generic_file_llseek,
  887. };