dir.c 20 KB

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