dir.c 23 KB

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