dir.c 22 KB

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