dir.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. /*
  2. * dir.c - Operations for sysfs directories.
  3. */
  4. #undef DEBUG
  5. #include <linux/fs.h>
  6. #include <linux/mount.h>
  7. #include <linux/module.h>
  8. #include <linux/kobject.h>
  9. #include <linux/namei.h>
  10. #include <linux/idr.h>
  11. #include <linux/completion.h>
  12. #include <asm/semaphore.h>
  13. #include "sysfs.h"
  14. DECLARE_RWSEM(sysfs_rename_sem);
  15. spinlock_t sysfs_lock = SPIN_LOCK_UNLOCKED;
  16. spinlock_t kobj_sysfs_assoc_lock = SPIN_LOCK_UNLOCKED;
  17. static spinlock_t sysfs_ino_lock = SPIN_LOCK_UNLOCKED;
  18. static DEFINE_IDA(sysfs_ino_ida);
  19. /**
  20. * sysfs_link_sibling - link sysfs_dirent into sibling list
  21. * @sd: sysfs_dirent of interest
  22. *
  23. * Link @sd into its sibling list which starts from
  24. * sd->s_parent->s_children.
  25. *
  26. * Locking:
  27. * mutex_lock(sd->s_parent->dentry->d_inode->i_mutex)
  28. */
  29. static void sysfs_link_sibling(struct sysfs_dirent *sd)
  30. {
  31. struct sysfs_dirent *parent_sd = sd->s_parent;
  32. BUG_ON(sd->s_sibling);
  33. sd->s_sibling = parent_sd->s_children;
  34. parent_sd->s_children = sd;
  35. }
  36. /**
  37. * sysfs_unlink_sibling - unlink sysfs_dirent from sibling list
  38. * @sd: sysfs_dirent of interest
  39. *
  40. * Unlink @sd from its sibling list which starts from
  41. * sd->s_parent->s_children.
  42. *
  43. * Locking:
  44. * mutex_lock(sd->s_parent->dentry->d_inode->i_mutex)
  45. */
  46. static void sysfs_unlink_sibling(struct sysfs_dirent *sd)
  47. {
  48. struct sysfs_dirent **pos;
  49. for (pos = &sd->s_parent->s_children; *pos; pos = &(*pos)->s_sibling) {
  50. if (*pos == sd) {
  51. *pos = sd->s_sibling;
  52. sd->s_sibling = NULL;
  53. break;
  54. }
  55. }
  56. }
  57. /**
  58. * sysfs_get_active - get an active reference to sysfs_dirent
  59. * @sd: sysfs_dirent to get an active reference to
  60. *
  61. * Get an active reference of @sd. This function is noop if @sd
  62. * is NULL.
  63. *
  64. * RETURNS:
  65. * Pointer to @sd on success, NULL on failure.
  66. */
  67. struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
  68. {
  69. if (unlikely(!sd))
  70. return NULL;
  71. while (1) {
  72. int v, t;
  73. v = atomic_read(&sd->s_active);
  74. if (unlikely(v < 0))
  75. return NULL;
  76. t = atomic_cmpxchg(&sd->s_active, v, v + 1);
  77. if (likely(t == v))
  78. return sd;
  79. if (t < 0)
  80. return NULL;
  81. cpu_relax();
  82. }
  83. }
  84. /**
  85. * sysfs_put_active - put an active reference to sysfs_dirent
  86. * @sd: sysfs_dirent to put an active reference to
  87. *
  88. * Put an active reference to @sd. This function is noop if @sd
  89. * is NULL.
  90. */
  91. void sysfs_put_active(struct sysfs_dirent *sd)
  92. {
  93. struct completion *cmpl;
  94. int v;
  95. if (unlikely(!sd))
  96. return;
  97. v = atomic_dec_return(&sd->s_active);
  98. if (likely(v != SD_DEACTIVATED_BIAS))
  99. return;
  100. /* atomic_dec_return() is a mb(), we'll always see the updated
  101. * sd->s_sibling.
  102. */
  103. cmpl = (void *)sd->s_sibling;
  104. complete(cmpl);
  105. }
  106. /**
  107. * sysfs_get_active_two - get active references to sysfs_dirent and parent
  108. * @sd: sysfs_dirent of interest
  109. *
  110. * Get active reference to @sd and its parent. Parent's active
  111. * reference is grabbed first. This function is noop if @sd is
  112. * NULL.
  113. *
  114. * RETURNS:
  115. * Pointer to @sd on success, NULL on failure.
  116. */
  117. struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd)
  118. {
  119. if (sd) {
  120. if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent)))
  121. return NULL;
  122. if (unlikely(!sysfs_get_active(sd))) {
  123. sysfs_put_active(sd->s_parent);
  124. return NULL;
  125. }
  126. }
  127. return sd;
  128. }
  129. /**
  130. * sysfs_put_active_two - put active references to sysfs_dirent and parent
  131. * @sd: sysfs_dirent of interest
  132. *
  133. * Put active references to @sd and its parent. This function is
  134. * noop if @sd is NULL.
  135. */
  136. void sysfs_put_active_two(struct sysfs_dirent *sd)
  137. {
  138. if (sd) {
  139. sysfs_put_active(sd);
  140. sysfs_put_active(sd->s_parent);
  141. }
  142. }
  143. /**
  144. * sysfs_deactivate - deactivate sysfs_dirent
  145. * @sd: sysfs_dirent to deactivate
  146. *
  147. * Deny new active references and drain existing ones.
  148. */
  149. void sysfs_deactivate(struct sysfs_dirent *sd)
  150. {
  151. DECLARE_COMPLETION_ONSTACK(wait);
  152. int v;
  153. BUG_ON(sd->s_sibling || !(sd->s_flags & SYSFS_FLAG_REMOVED));
  154. sd->s_sibling = (void *)&wait;
  155. /* atomic_add_return() is a mb(), put_active() will always see
  156. * the updated sd->s_sibling.
  157. */
  158. v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
  159. if (v != SD_DEACTIVATED_BIAS)
  160. wait_for_completion(&wait);
  161. sd->s_sibling = NULL;
  162. }
  163. static int sysfs_alloc_ino(ino_t *pino)
  164. {
  165. int ino, rc;
  166. retry:
  167. spin_lock(&sysfs_ino_lock);
  168. rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
  169. spin_unlock(&sysfs_ino_lock);
  170. if (rc == -EAGAIN) {
  171. if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
  172. goto retry;
  173. rc = -ENOMEM;
  174. }
  175. *pino = ino;
  176. return rc;
  177. }
  178. static void sysfs_free_ino(ino_t ino)
  179. {
  180. spin_lock(&sysfs_ino_lock);
  181. ida_remove(&sysfs_ino_ida, ino);
  182. spin_unlock(&sysfs_ino_lock);
  183. }
  184. void release_sysfs_dirent(struct sysfs_dirent * sd)
  185. {
  186. struct sysfs_dirent *parent_sd;
  187. repeat:
  188. parent_sd = sd->s_parent;
  189. if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
  190. sysfs_put(sd->s_elem.symlink.target_sd);
  191. if (sysfs_type(sd) & SYSFS_COPY_NAME)
  192. kfree(sd->s_name);
  193. kfree(sd->s_iattr);
  194. sysfs_free_ino(sd->s_ino);
  195. kmem_cache_free(sysfs_dir_cachep, sd);
  196. sd = parent_sd;
  197. if (sd && atomic_dec_and_test(&sd->s_count))
  198. goto repeat;
  199. }
  200. static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
  201. {
  202. struct sysfs_dirent * sd = dentry->d_fsdata;
  203. if (sd) {
  204. /* sd->s_dentry is protected with sysfs_lock. This
  205. * allows sysfs_drop_dentry() to dereference it.
  206. */
  207. spin_lock(&sysfs_lock);
  208. /* The dentry might have been deleted or another
  209. * lookup could have happened updating sd->s_dentry to
  210. * point the new dentry. Ignore if it isn't pointing
  211. * to this dentry.
  212. */
  213. if (sd->s_dentry == dentry)
  214. sd->s_dentry = NULL;
  215. spin_unlock(&sysfs_lock);
  216. sysfs_put(sd);
  217. }
  218. iput(inode);
  219. }
  220. static struct dentry_operations sysfs_dentry_ops = {
  221. .d_iput = sysfs_d_iput,
  222. };
  223. struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
  224. {
  225. char *dup_name = NULL;
  226. struct sysfs_dirent *sd = NULL;
  227. if (type & SYSFS_COPY_NAME) {
  228. name = dup_name = kstrdup(name, GFP_KERNEL);
  229. if (!name)
  230. goto err_out;
  231. }
  232. sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
  233. if (!sd)
  234. goto err_out;
  235. if (sysfs_alloc_ino(&sd->s_ino))
  236. goto err_out;
  237. atomic_set(&sd->s_count, 1);
  238. atomic_set(&sd->s_active, 0);
  239. atomic_set(&sd->s_event, 1);
  240. sd->s_name = name;
  241. sd->s_mode = mode;
  242. sd->s_flags = type;
  243. return sd;
  244. err_out:
  245. kfree(dup_name);
  246. kmem_cache_free(sysfs_dir_cachep, sd);
  247. return NULL;
  248. }
  249. static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry)
  250. {
  251. dentry->d_op = &sysfs_dentry_ops;
  252. dentry->d_fsdata = sysfs_get(sd);
  253. /* protect sd->s_dentry against sysfs_d_iput */
  254. spin_lock(&sysfs_lock);
  255. sd->s_dentry = dentry;
  256. spin_unlock(&sysfs_lock);
  257. d_rehash(dentry);
  258. }
  259. void sysfs_attach_dirent(struct sysfs_dirent *sd,
  260. struct sysfs_dirent *parent_sd, struct dentry *dentry)
  261. {
  262. if (dentry)
  263. sysfs_attach_dentry(sd, dentry);
  264. if (parent_sd) {
  265. sd->s_parent = sysfs_get(parent_sd);
  266. sysfs_link_sibling(sd);
  267. }
  268. }
  269. /*
  270. *
  271. * Return -EEXIST if there is already a sysfs element with the same name for
  272. * the same parent.
  273. *
  274. * called with parent inode's i_mutex held
  275. */
  276. int sysfs_dirent_exist(struct sysfs_dirent *parent_sd,
  277. const unsigned char *new)
  278. {
  279. struct sysfs_dirent * sd;
  280. for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) {
  281. if (sysfs_type(sd)) {
  282. if (strcmp(sd->s_name, new))
  283. continue;
  284. else
  285. return -EEXIST;
  286. }
  287. }
  288. return 0;
  289. }
  290. static int create_dir(struct kobject *kobj, struct dentry *parent,
  291. const char *name, struct dentry **p_dentry)
  292. {
  293. int error;
  294. umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
  295. struct dentry *dentry;
  296. struct inode *inode;
  297. struct sysfs_dirent *sd;
  298. mutex_lock(&parent->d_inode->i_mutex);
  299. /* allocate */
  300. dentry = lookup_one_len(name, parent, strlen(name));
  301. if (IS_ERR(dentry)) {
  302. error = PTR_ERR(dentry);
  303. goto out_unlock;
  304. }
  305. error = -EEXIST;
  306. if (dentry->d_inode)
  307. goto out_dput;
  308. error = -ENOMEM;
  309. sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
  310. if (!sd)
  311. goto out_drop;
  312. sd->s_elem.dir.kobj = kobj;
  313. inode = sysfs_get_inode(sd);
  314. if (!inode)
  315. goto out_sput;
  316. if (inode->i_state & I_NEW) {
  317. inode->i_op = &sysfs_dir_inode_operations;
  318. inode->i_fop = &sysfs_dir_operations;
  319. /* directory inodes start off with i_nlink == 2 (for ".") */
  320. inc_nlink(inode);
  321. }
  322. /* link in */
  323. error = -EEXIST;
  324. if (sysfs_dirent_exist(parent->d_fsdata, name))
  325. goto out_iput;
  326. sysfs_instantiate(dentry, inode);
  327. inc_nlink(parent->d_inode);
  328. sysfs_attach_dirent(sd, parent->d_fsdata, dentry);
  329. *p_dentry = dentry;
  330. error = 0;
  331. goto out_unlock; /* pin directory dentry in core */
  332. out_iput:
  333. iput(inode);
  334. out_sput:
  335. sysfs_put(sd);
  336. out_drop:
  337. d_drop(dentry);
  338. out_dput:
  339. dput(dentry);
  340. out_unlock:
  341. mutex_unlock(&parent->d_inode->i_mutex);
  342. return error;
  343. }
  344. int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d)
  345. {
  346. return create_dir(k,k->dentry,n,d);
  347. }
  348. /**
  349. * sysfs_create_dir - create a directory for an object.
  350. * @kobj: object we're creating directory for.
  351. * @shadow_parent: parent parent object.
  352. */
  353. int sysfs_create_dir(struct kobject * kobj, struct dentry *shadow_parent)
  354. {
  355. struct dentry * dentry = NULL;
  356. struct dentry * parent;
  357. int error = 0;
  358. BUG_ON(!kobj);
  359. if (shadow_parent)
  360. parent = shadow_parent;
  361. else if (kobj->parent)
  362. parent = kobj->parent->dentry;
  363. else if (sysfs_mount && sysfs_mount->mnt_sb)
  364. parent = sysfs_mount->mnt_sb->s_root;
  365. else
  366. return -EFAULT;
  367. error = create_dir(kobj,parent,kobject_name(kobj),&dentry);
  368. if (!error)
  369. kobj->dentry = dentry;
  370. return error;
  371. }
  372. static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
  373. struct nameidata *nd)
  374. {
  375. struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
  376. struct sysfs_dirent * sd;
  377. struct bin_attribute *bin_attr;
  378. struct inode *inode;
  379. int found = 0;
  380. for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) {
  381. if ((sysfs_type(sd) & SYSFS_NOT_PINNED) &&
  382. !strcmp(sd->s_name, dentry->d_name.name)) {
  383. found = 1;
  384. break;
  385. }
  386. }
  387. /* no such entry */
  388. if (!found)
  389. return NULL;
  390. /* attach dentry and inode */
  391. inode = sysfs_get_inode(sd);
  392. if (!inode)
  393. return ERR_PTR(-ENOMEM);
  394. if (inode->i_state & I_NEW) {
  395. /* initialize inode according to type */
  396. switch (sysfs_type(sd)) {
  397. case SYSFS_KOBJ_ATTR:
  398. inode->i_size = PAGE_SIZE;
  399. inode->i_fop = &sysfs_file_operations;
  400. break;
  401. case SYSFS_KOBJ_BIN_ATTR:
  402. bin_attr = sd->s_elem.bin_attr.bin_attr;
  403. inode->i_size = bin_attr->size;
  404. inode->i_fop = &bin_fops;
  405. break;
  406. case SYSFS_KOBJ_LINK:
  407. inode->i_op = &sysfs_symlink_inode_operations;
  408. break;
  409. default:
  410. BUG();
  411. }
  412. }
  413. sysfs_instantiate(dentry, inode);
  414. sysfs_attach_dentry(sd, dentry);
  415. return NULL;
  416. }
  417. const struct inode_operations sysfs_dir_inode_operations = {
  418. .lookup = sysfs_lookup,
  419. .setattr = sysfs_setattr,
  420. };
  421. static void remove_dir(struct dentry * d)
  422. {
  423. struct dentry *parent = d->d_parent;
  424. struct sysfs_dirent *sd = d->d_fsdata;
  425. mutex_lock(&parent->d_inode->i_mutex);
  426. sysfs_unlink_sibling(sd);
  427. sd->s_flags |= SYSFS_FLAG_REMOVED;
  428. pr_debug(" o %s removing done (%d)\n",d->d_name.name,
  429. atomic_read(&d->d_count));
  430. mutex_unlock(&parent->d_inode->i_mutex);
  431. sysfs_drop_dentry(sd);
  432. sysfs_deactivate(sd);
  433. sysfs_put(sd);
  434. }
  435. void sysfs_remove_subdir(struct dentry * d)
  436. {
  437. remove_dir(d);
  438. }
  439. static void __sysfs_remove_dir(struct dentry *dentry)
  440. {
  441. struct sysfs_dirent *removed = NULL;
  442. struct sysfs_dirent *parent_sd;
  443. struct sysfs_dirent **pos;
  444. if (!dentry)
  445. return;
  446. pr_debug("sysfs %s: removing dir\n",dentry->d_name.name);
  447. mutex_lock(&dentry->d_inode->i_mutex);
  448. parent_sd = dentry->d_fsdata;
  449. pos = &parent_sd->s_children;
  450. while (*pos) {
  451. struct sysfs_dirent *sd = *pos;
  452. if (sysfs_type(sd) && (sysfs_type(sd) & SYSFS_NOT_PINNED)) {
  453. sd->s_flags |= SYSFS_FLAG_REMOVED;
  454. *pos = sd->s_sibling;
  455. sd->s_sibling = removed;
  456. removed = sd;
  457. } else
  458. pos = &(*pos)->s_sibling;
  459. }
  460. mutex_unlock(&dentry->d_inode->i_mutex);
  461. while (removed) {
  462. struct sysfs_dirent *sd = removed;
  463. removed = sd->s_sibling;
  464. sd->s_sibling = NULL;
  465. sysfs_drop_dentry(sd);
  466. sysfs_deactivate(sd);
  467. sysfs_put(sd);
  468. }
  469. remove_dir(dentry);
  470. }
  471. /**
  472. * sysfs_remove_dir - remove an object's directory.
  473. * @kobj: object.
  474. *
  475. * The only thing special about this is that we remove any files in
  476. * the directory before we remove the directory, and we've inlined
  477. * what used to be sysfs_rmdir() below, instead of calling separately.
  478. */
  479. void sysfs_remove_dir(struct kobject * kobj)
  480. {
  481. struct dentry *d = kobj->dentry;
  482. spin_lock(&kobj_sysfs_assoc_lock);
  483. kobj->dentry = NULL;
  484. spin_unlock(&kobj_sysfs_assoc_lock);
  485. __sysfs_remove_dir(d);
  486. }
  487. int sysfs_rename_dir(struct kobject * kobj, struct dentry *new_parent,
  488. const char *new_name)
  489. {
  490. struct sysfs_dirent *sd = kobj->dentry->d_fsdata;
  491. struct sysfs_dirent *parent_sd = new_parent->d_fsdata;
  492. struct dentry *new_dentry;
  493. char *dup_name;
  494. int error;
  495. if (!new_parent)
  496. return -EFAULT;
  497. down_write(&sysfs_rename_sem);
  498. mutex_lock(&new_parent->d_inode->i_mutex);
  499. new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
  500. if (IS_ERR(new_dentry)) {
  501. error = PTR_ERR(new_dentry);
  502. goto out_unlock;
  503. }
  504. /* By allowing two different directories with the same
  505. * d_parent we allow this routine to move between different
  506. * shadows of the same directory
  507. */
  508. error = -EINVAL;
  509. if (kobj->dentry->d_parent->d_inode != new_parent->d_inode ||
  510. new_dentry->d_parent->d_inode != new_parent->d_inode ||
  511. new_dentry == kobj->dentry)
  512. goto out_dput;
  513. error = -EEXIST;
  514. if (new_dentry->d_inode)
  515. goto out_dput;
  516. /* rename kobject and sysfs_dirent */
  517. error = -ENOMEM;
  518. new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
  519. if (!new_name)
  520. goto out_drop;
  521. error = kobject_set_name(kobj, "%s", new_name);
  522. if (error)
  523. goto out_free;
  524. kfree(sd->s_name);
  525. sd->s_name = new_name;
  526. /* move under the new parent */
  527. d_add(new_dentry, NULL);
  528. d_move(kobj->dentry, new_dentry);
  529. sysfs_unlink_sibling(sd);
  530. sysfs_get(parent_sd);
  531. sysfs_put(sd->s_parent);
  532. sd->s_parent = parent_sd;
  533. sysfs_link_sibling(sd);
  534. error = 0;
  535. goto out_unlock;
  536. out_free:
  537. kfree(dup_name);
  538. out_drop:
  539. d_drop(new_dentry);
  540. out_dput:
  541. dput(new_dentry);
  542. out_unlock:
  543. mutex_unlock(&new_parent->d_inode->i_mutex);
  544. up_write(&sysfs_rename_sem);
  545. return error;
  546. }
  547. int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
  548. {
  549. struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
  550. struct sysfs_dirent *new_parent_sd, *sd;
  551. int error;
  552. old_parent_dentry = kobj->parent ?
  553. kobj->parent->dentry : sysfs_mount->mnt_sb->s_root;
  554. new_parent_dentry = new_parent ?
  555. new_parent->dentry : sysfs_mount->mnt_sb->s_root;
  556. if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
  557. return 0; /* nothing to move */
  558. again:
  559. mutex_lock(&old_parent_dentry->d_inode->i_mutex);
  560. if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
  561. mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
  562. goto again;
  563. }
  564. new_parent_sd = new_parent_dentry->d_fsdata;
  565. sd = kobj->dentry->d_fsdata;
  566. new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
  567. strlen(kobj->name));
  568. if (IS_ERR(new_dentry)) {
  569. error = PTR_ERR(new_dentry);
  570. goto out;
  571. } else
  572. error = 0;
  573. d_add(new_dentry, NULL);
  574. d_move(kobj->dentry, new_dentry);
  575. dput(new_dentry);
  576. /* Remove from old parent's list and insert into new parent's list. */
  577. sysfs_unlink_sibling(sd);
  578. sysfs_get(new_parent_sd);
  579. sysfs_put(sd->s_parent);
  580. sd->s_parent = new_parent_sd;
  581. sysfs_link_sibling(sd);
  582. out:
  583. mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
  584. mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
  585. return error;
  586. }
  587. static int sysfs_dir_open(struct inode *inode, struct file *file)
  588. {
  589. struct dentry * dentry = file->f_path.dentry;
  590. struct sysfs_dirent * parent_sd = dentry->d_fsdata;
  591. struct sysfs_dirent * sd;
  592. mutex_lock(&dentry->d_inode->i_mutex);
  593. sd = sysfs_new_dirent("_DIR_", 0, 0);
  594. if (sd)
  595. sysfs_attach_dirent(sd, parent_sd, NULL);
  596. mutex_unlock(&dentry->d_inode->i_mutex);
  597. file->private_data = sd;
  598. return sd ? 0 : -ENOMEM;
  599. }
  600. static int sysfs_dir_close(struct inode *inode, struct file *file)
  601. {
  602. struct dentry * dentry = file->f_path.dentry;
  603. struct sysfs_dirent * cursor = file->private_data;
  604. mutex_lock(&dentry->d_inode->i_mutex);
  605. sysfs_unlink_sibling(cursor);
  606. mutex_unlock(&dentry->d_inode->i_mutex);
  607. release_sysfs_dirent(cursor);
  608. return 0;
  609. }
  610. /* Relationship between s_mode and the DT_xxx types */
  611. static inline unsigned char dt_type(struct sysfs_dirent *sd)
  612. {
  613. return (sd->s_mode >> 12) & 15;
  614. }
  615. static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
  616. {
  617. struct dentry *dentry = filp->f_path.dentry;
  618. struct sysfs_dirent * parent_sd = dentry->d_fsdata;
  619. struct sysfs_dirent *cursor = filp->private_data;
  620. struct sysfs_dirent **pos;
  621. ino_t ino;
  622. int i = filp->f_pos;
  623. switch (i) {
  624. case 0:
  625. ino = parent_sd->s_ino;
  626. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  627. break;
  628. filp->f_pos++;
  629. i++;
  630. /* fallthrough */
  631. case 1:
  632. if (parent_sd->s_parent)
  633. ino = parent_sd->s_parent->s_ino;
  634. else
  635. ino = parent_sd->s_ino;
  636. if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
  637. break;
  638. filp->f_pos++;
  639. i++;
  640. /* fallthrough */
  641. default:
  642. pos = &parent_sd->s_children;
  643. while (*pos != cursor)
  644. pos = &(*pos)->s_sibling;
  645. /* unlink cursor */
  646. *pos = cursor->s_sibling;
  647. if (filp->f_pos == 2)
  648. pos = &parent_sd->s_children;
  649. for ( ; *pos; pos = &(*pos)->s_sibling) {
  650. struct sysfs_dirent *next = *pos;
  651. const char * name;
  652. int len;
  653. if (!sysfs_type(next))
  654. continue;
  655. name = next->s_name;
  656. len = strlen(name);
  657. ino = next->s_ino;
  658. if (filldir(dirent, name, len, filp->f_pos, ino,
  659. dt_type(next)) < 0)
  660. break;
  661. filp->f_pos++;
  662. }
  663. /* put cursor back in */
  664. cursor->s_sibling = *pos;
  665. *pos = cursor;
  666. }
  667. return 0;
  668. }
  669. static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
  670. {
  671. struct dentry * dentry = file->f_path.dentry;
  672. mutex_lock(&dentry->d_inode->i_mutex);
  673. switch (origin) {
  674. case 1:
  675. offset += file->f_pos;
  676. case 0:
  677. if (offset >= 0)
  678. break;
  679. default:
  680. mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
  681. return -EINVAL;
  682. }
  683. if (offset != file->f_pos) {
  684. file->f_pos = offset;
  685. if (file->f_pos >= 2) {
  686. struct sysfs_dirent *sd = dentry->d_fsdata;
  687. struct sysfs_dirent *cursor = file->private_data;
  688. struct sysfs_dirent **pos;
  689. loff_t n = file->f_pos - 2;
  690. sysfs_unlink_sibling(cursor);
  691. pos = &sd->s_children;
  692. while (n && *pos) {
  693. struct sysfs_dirent *next = *pos;
  694. if (sysfs_type(next))
  695. n--;
  696. pos = &(*pos)->s_sibling;
  697. }
  698. cursor->s_sibling = *pos;
  699. *pos = cursor;
  700. }
  701. }
  702. mutex_unlock(&dentry->d_inode->i_mutex);
  703. return offset;
  704. }
  705. /**
  706. * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
  707. * @kobj: object we're creating shadow of.
  708. */
  709. int sysfs_make_shadowed_dir(struct kobject *kobj,
  710. void * (*follow_link)(struct dentry *, struct nameidata *))
  711. {
  712. struct inode *inode;
  713. struct inode_operations *i_op;
  714. inode = kobj->dentry->d_inode;
  715. if (inode->i_op != &sysfs_dir_inode_operations)
  716. return -EINVAL;
  717. i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
  718. if (!i_op)
  719. return -ENOMEM;
  720. memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
  721. i_op->follow_link = follow_link;
  722. /* Locking of inode->i_op?
  723. * Since setting i_op is a single word write and they
  724. * are atomic we should be ok here.
  725. */
  726. inode->i_op = i_op;
  727. return 0;
  728. }
  729. /**
  730. * sysfs_create_shadow_dir - create a shadow directory for an object.
  731. * @kobj: object we're creating directory for.
  732. *
  733. * sysfs_make_shadowed_dir must already have been called on this
  734. * directory.
  735. */
  736. struct dentry *sysfs_create_shadow_dir(struct kobject *kobj)
  737. {
  738. struct dentry *dir = kobj->dentry;
  739. struct inode *inode = dir->d_inode;
  740. struct dentry *parent = dir->d_parent;
  741. struct sysfs_dirent *parent_sd = parent->d_fsdata;
  742. struct dentry *shadow;
  743. struct sysfs_dirent *sd;
  744. shadow = ERR_PTR(-EINVAL);
  745. if (!sysfs_is_shadowed_inode(inode))
  746. goto out;
  747. shadow = d_alloc(parent, &dir->d_name);
  748. if (!shadow)
  749. goto nomem;
  750. sd = sysfs_new_dirent("_SHADOW_", inode->i_mode, SYSFS_DIR);
  751. if (!sd)
  752. goto nomem;
  753. sd->s_elem.dir.kobj = kobj;
  754. /* point to parent_sd but don't attach to it */
  755. sd->s_parent = sysfs_get(parent_sd);
  756. sysfs_attach_dirent(sd, NULL, shadow);
  757. d_instantiate(shadow, igrab(inode));
  758. inc_nlink(inode);
  759. inc_nlink(parent->d_inode);
  760. dget(shadow); /* Extra count - pin the dentry in core */
  761. out:
  762. return shadow;
  763. nomem:
  764. dput(shadow);
  765. shadow = ERR_PTR(-ENOMEM);
  766. goto out;
  767. }
  768. /**
  769. * sysfs_remove_shadow_dir - remove an object's directory.
  770. * @shadow: dentry of shadow directory
  771. *
  772. * The only thing special about this is that we remove any files in
  773. * the directory before we remove the directory, and we've inlined
  774. * what used to be sysfs_rmdir() below, instead of calling separately.
  775. */
  776. void sysfs_remove_shadow_dir(struct dentry *shadow)
  777. {
  778. __sysfs_remove_dir(shadow);
  779. }
  780. const struct file_operations sysfs_dir_operations = {
  781. .open = sysfs_dir_open,
  782. .release = sysfs_dir_close,
  783. .llseek = sysfs_dir_lseek,
  784. .read = generic_read_dir,
  785. .readdir = sysfs_readdir,
  786. };