dir.c 17 KB

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