dir.c 18 KB

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