dir.c 24 KB

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