dir.c 24 KB

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