dir.c 23 KB

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