dir.c 24 KB

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