dir.c 22 KB

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