dir.c 26 KB

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