dir.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  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. * sysfs_find_dirent - find sysfs_dirent with the given name
  271. * @parent_sd: sysfs_dirent to search under
  272. * @name: name to look for
  273. *
  274. * Look for sysfs_dirent with name @name under @parent_sd.
  275. *
  276. * LOCKING:
  277. * mutex_lock(parent->i_mutex)
  278. *
  279. * RETURNS:
  280. * Pointer to sysfs_dirent if found, NULL if not.
  281. */
  282. struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
  283. const unsigned char *name)
  284. {
  285. struct sysfs_dirent *sd;
  286. for (sd = parent_sd->s_children; sd; sd = sd->s_sibling)
  287. if (sysfs_type(sd) && !strcmp(sd->s_name, name))
  288. return sd;
  289. return NULL;
  290. }
  291. /**
  292. * sysfs_get_dirent - find and get sysfs_dirent with the given name
  293. * @parent_sd: sysfs_dirent to search under
  294. * @name: name to look for
  295. *
  296. * Look for sysfs_dirent with name @name under @parent_sd and get
  297. * it if found.
  298. *
  299. * LOCKING:
  300. * Kernel thread context (may sleep)
  301. *
  302. * RETURNS:
  303. * Pointer to sysfs_dirent if found, NULL if not.
  304. */
  305. struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
  306. const unsigned char *name)
  307. {
  308. struct sysfs_dirent *sd;
  309. mutex_lock(&parent_sd->s_dentry->d_inode->i_mutex);
  310. sd = sysfs_find_dirent(parent_sd, name);
  311. sysfs_get(sd);
  312. mutex_unlock(&parent_sd->s_dentry->d_inode->i_mutex);
  313. return sd;
  314. }
  315. static int create_dir(struct kobject *kobj, struct dentry *parent,
  316. const char *name, struct dentry **p_dentry)
  317. {
  318. int error;
  319. umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
  320. struct dentry *dentry;
  321. struct inode *inode;
  322. struct sysfs_dirent *sd;
  323. mutex_lock(&parent->d_inode->i_mutex);
  324. /* allocate */
  325. dentry = lookup_one_len(name, parent, strlen(name));
  326. if (IS_ERR(dentry)) {
  327. error = PTR_ERR(dentry);
  328. goto out_unlock;
  329. }
  330. error = -EEXIST;
  331. if (dentry->d_inode)
  332. goto out_dput;
  333. error = -ENOMEM;
  334. sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
  335. if (!sd)
  336. goto out_drop;
  337. sd->s_elem.dir.kobj = kobj;
  338. inode = sysfs_get_inode(sd);
  339. if (!inode)
  340. goto out_sput;
  341. if (inode->i_state & I_NEW) {
  342. inode->i_op = &sysfs_dir_inode_operations;
  343. inode->i_fop = &sysfs_dir_operations;
  344. /* directory inodes start off with i_nlink == 2 (for ".") */
  345. inc_nlink(inode);
  346. }
  347. /* link in */
  348. error = -EEXIST;
  349. if (sysfs_find_dirent(parent->d_fsdata, name))
  350. goto out_iput;
  351. sysfs_instantiate(dentry, inode);
  352. inc_nlink(parent->d_inode);
  353. sysfs_attach_dirent(sd, parent->d_fsdata, dentry);
  354. *p_dentry = dentry;
  355. error = 0;
  356. goto out_unlock; /* pin directory dentry in core */
  357. out_iput:
  358. iput(inode);
  359. out_sput:
  360. sysfs_put(sd);
  361. out_drop:
  362. d_drop(dentry);
  363. out_dput:
  364. dput(dentry);
  365. out_unlock:
  366. mutex_unlock(&parent->d_inode->i_mutex);
  367. return error;
  368. }
  369. int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d)
  370. {
  371. return create_dir(k,k->dentry,n,d);
  372. }
  373. /**
  374. * sysfs_create_dir - create a directory for an object.
  375. * @kobj: object we're creating directory for.
  376. * @shadow_parent: parent parent object.
  377. */
  378. int sysfs_create_dir(struct kobject * kobj, struct dentry *shadow_parent)
  379. {
  380. struct dentry * dentry = NULL;
  381. struct dentry * parent;
  382. int error = 0;
  383. BUG_ON(!kobj);
  384. if (shadow_parent)
  385. parent = shadow_parent;
  386. else if (kobj->parent)
  387. parent = kobj->parent->dentry;
  388. else if (sysfs_mount && sysfs_mount->mnt_sb)
  389. parent = sysfs_mount->mnt_sb->s_root;
  390. else
  391. return -EFAULT;
  392. error = create_dir(kobj,parent,kobject_name(kobj),&dentry);
  393. if (!error)
  394. kobj->dentry = dentry;
  395. return error;
  396. }
  397. static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
  398. struct nameidata *nd)
  399. {
  400. struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
  401. struct sysfs_dirent * sd;
  402. struct bin_attribute *bin_attr;
  403. struct inode *inode;
  404. int found = 0;
  405. for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) {
  406. if ((sysfs_type(sd) & SYSFS_NOT_PINNED) &&
  407. !strcmp(sd->s_name, dentry->d_name.name)) {
  408. found = 1;
  409. break;
  410. }
  411. }
  412. /* no such entry */
  413. if (!found)
  414. return NULL;
  415. /* attach dentry and inode */
  416. inode = sysfs_get_inode(sd);
  417. if (!inode)
  418. return ERR_PTR(-ENOMEM);
  419. if (inode->i_state & I_NEW) {
  420. /* initialize inode according to type */
  421. switch (sysfs_type(sd)) {
  422. case SYSFS_KOBJ_ATTR:
  423. inode->i_size = PAGE_SIZE;
  424. inode->i_fop = &sysfs_file_operations;
  425. break;
  426. case SYSFS_KOBJ_BIN_ATTR:
  427. bin_attr = sd->s_elem.bin_attr.bin_attr;
  428. inode->i_size = bin_attr->size;
  429. inode->i_fop = &bin_fops;
  430. break;
  431. case SYSFS_KOBJ_LINK:
  432. inode->i_op = &sysfs_symlink_inode_operations;
  433. break;
  434. default:
  435. BUG();
  436. }
  437. }
  438. sysfs_instantiate(dentry, inode);
  439. sysfs_attach_dentry(sd, dentry);
  440. return NULL;
  441. }
  442. const struct inode_operations sysfs_dir_inode_operations = {
  443. .lookup = sysfs_lookup,
  444. .setattr = sysfs_setattr,
  445. };
  446. static void remove_dir(struct dentry * d)
  447. {
  448. struct dentry *parent = d->d_parent;
  449. struct sysfs_dirent *sd = d->d_fsdata;
  450. mutex_lock(&parent->d_inode->i_mutex);
  451. sysfs_unlink_sibling(sd);
  452. sd->s_flags |= SYSFS_FLAG_REMOVED;
  453. pr_debug(" o %s removing done (%d)\n",d->d_name.name,
  454. atomic_read(&d->d_count));
  455. mutex_unlock(&parent->d_inode->i_mutex);
  456. sysfs_drop_dentry(sd);
  457. sysfs_deactivate(sd);
  458. sysfs_put(sd);
  459. }
  460. void sysfs_remove_subdir(struct dentry * d)
  461. {
  462. remove_dir(d);
  463. }
  464. static void __sysfs_remove_dir(struct dentry *dentry)
  465. {
  466. struct sysfs_dirent *removed = NULL;
  467. struct sysfs_dirent *parent_sd;
  468. struct sysfs_dirent **pos;
  469. if (!dentry)
  470. return;
  471. pr_debug("sysfs %s: removing dir\n",dentry->d_name.name);
  472. mutex_lock(&dentry->d_inode->i_mutex);
  473. parent_sd = dentry->d_fsdata;
  474. pos = &parent_sd->s_children;
  475. while (*pos) {
  476. struct sysfs_dirent *sd = *pos;
  477. if (sysfs_type(sd) && (sysfs_type(sd) & SYSFS_NOT_PINNED)) {
  478. sd->s_flags |= SYSFS_FLAG_REMOVED;
  479. *pos = sd->s_sibling;
  480. sd->s_sibling = removed;
  481. removed = sd;
  482. } else
  483. pos = &(*pos)->s_sibling;
  484. }
  485. mutex_unlock(&dentry->d_inode->i_mutex);
  486. while (removed) {
  487. struct sysfs_dirent *sd = removed;
  488. removed = sd->s_sibling;
  489. sd->s_sibling = NULL;
  490. sysfs_drop_dentry(sd);
  491. sysfs_deactivate(sd);
  492. sysfs_put(sd);
  493. }
  494. remove_dir(dentry);
  495. }
  496. /**
  497. * sysfs_remove_dir - remove an object's directory.
  498. * @kobj: object.
  499. *
  500. * The only thing special about this is that we remove any files in
  501. * the directory before we remove the directory, and we've inlined
  502. * what used to be sysfs_rmdir() below, instead of calling separately.
  503. */
  504. void sysfs_remove_dir(struct kobject * kobj)
  505. {
  506. struct dentry *d = kobj->dentry;
  507. spin_lock(&kobj_sysfs_assoc_lock);
  508. kobj->dentry = NULL;
  509. spin_unlock(&kobj_sysfs_assoc_lock);
  510. __sysfs_remove_dir(d);
  511. }
  512. int sysfs_rename_dir(struct kobject * kobj, struct dentry *new_parent,
  513. const char *new_name)
  514. {
  515. struct sysfs_dirent *sd = kobj->dentry->d_fsdata;
  516. struct sysfs_dirent *parent_sd = new_parent->d_fsdata;
  517. struct dentry *new_dentry;
  518. char *dup_name;
  519. int error;
  520. if (!new_parent)
  521. return -EFAULT;
  522. down_write(&sysfs_rename_sem);
  523. mutex_lock(&new_parent->d_inode->i_mutex);
  524. new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
  525. if (IS_ERR(new_dentry)) {
  526. error = PTR_ERR(new_dentry);
  527. goto out_unlock;
  528. }
  529. /* By allowing two different directories with the same
  530. * d_parent we allow this routine to move between different
  531. * shadows of the same directory
  532. */
  533. error = -EINVAL;
  534. if (kobj->dentry->d_parent->d_inode != new_parent->d_inode ||
  535. new_dentry->d_parent->d_inode != new_parent->d_inode ||
  536. new_dentry == kobj->dentry)
  537. goto out_dput;
  538. error = -EEXIST;
  539. if (new_dentry->d_inode)
  540. goto out_dput;
  541. /* rename kobject and sysfs_dirent */
  542. error = -ENOMEM;
  543. new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
  544. if (!new_name)
  545. goto out_drop;
  546. error = kobject_set_name(kobj, "%s", new_name);
  547. if (error)
  548. goto out_free;
  549. kfree(sd->s_name);
  550. sd->s_name = new_name;
  551. /* move under the new parent */
  552. d_add(new_dentry, NULL);
  553. d_move(kobj->dentry, new_dentry);
  554. sysfs_unlink_sibling(sd);
  555. sysfs_get(parent_sd);
  556. sysfs_put(sd->s_parent);
  557. sd->s_parent = parent_sd;
  558. sysfs_link_sibling(sd);
  559. error = 0;
  560. goto out_unlock;
  561. out_free:
  562. kfree(dup_name);
  563. out_drop:
  564. d_drop(new_dentry);
  565. out_dput:
  566. dput(new_dentry);
  567. out_unlock:
  568. mutex_unlock(&new_parent->d_inode->i_mutex);
  569. up_write(&sysfs_rename_sem);
  570. return error;
  571. }
  572. int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
  573. {
  574. struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
  575. struct sysfs_dirent *new_parent_sd, *sd;
  576. int error;
  577. old_parent_dentry = kobj->parent ?
  578. kobj->parent->dentry : sysfs_mount->mnt_sb->s_root;
  579. new_parent_dentry = new_parent ?
  580. new_parent->dentry : sysfs_mount->mnt_sb->s_root;
  581. if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
  582. return 0; /* nothing to move */
  583. again:
  584. mutex_lock(&old_parent_dentry->d_inode->i_mutex);
  585. if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
  586. mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
  587. goto again;
  588. }
  589. new_parent_sd = new_parent_dentry->d_fsdata;
  590. sd = kobj->dentry->d_fsdata;
  591. new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
  592. strlen(kobj->name));
  593. if (IS_ERR(new_dentry)) {
  594. error = PTR_ERR(new_dentry);
  595. goto out;
  596. } else
  597. error = 0;
  598. d_add(new_dentry, NULL);
  599. d_move(kobj->dentry, new_dentry);
  600. dput(new_dentry);
  601. /* Remove from old parent's list and insert into new parent's list. */
  602. sysfs_unlink_sibling(sd);
  603. sysfs_get(new_parent_sd);
  604. sysfs_put(sd->s_parent);
  605. sd->s_parent = new_parent_sd;
  606. sysfs_link_sibling(sd);
  607. out:
  608. mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
  609. mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
  610. return error;
  611. }
  612. static int sysfs_dir_open(struct inode *inode, struct file *file)
  613. {
  614. struct dentry * dentry = file->f_path.dentry;
  615. struct sysfs_dirent * parent_sd = dentry->d_fsdata;
  616. struct sysfs_dirent * sd;
  617. mutex_lock(&dentry->d_inode->i_mutex);
  618. sd = sysfs_new_dirent("_DIR_", 0, 0);
  619. if (sd)
  620. sysfs_attach_dirent(sd, parent_sd, NULL);
  621. mutex_unlock(&dentry->d_inode->i_mutex);
  622. file->private_data = sd;
  623. return sd ? 0 : -ENOMEM;
  624. }
  625. static int sysfs_dir_close(struct inode *inode, struct file *file)
  626. {
  627. struct dentry * dentry = file->f_path.dentry;
  628. struct sysfs_dirent * cursor = file->private_data;
  629. mutex_lock(&dentry->d_inode->i_mutex);
  630. sysfs_unlink_sibling(cursor);
  631. mutex_unlock(&dentry->d_inode->i_mutex);
  632. release_sysfs_dirent(cursor);
  633. return 0;
  634. }
  635. /* Relationship between s_mode and the DT_xxx types */
  636. static inline unsigned char dt_type(struct sysfs_dirent *sd)
  637. {
  638. return (sd->s_mode >> 12) & 15;
  639. }
  640. static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
  641. {
  642. struct dentry *dentry = filp->f_path.dentry;
  643. struct sysfs_dirent * parent_sd = dentry->d_fsdata;
  644. struct sysfs_dirent *cursor = filp->private_data;
  645. struct sysfs_dirent **pos;
  646. ino_t ino;
  647. int i = filp->f_pos;
  648. switch (i) {
  649. case 0:
  650. ino = parent_sd->s_ino;
  651. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  652. break;
  653. filp->f_pos++;
  654. i++;
  655. /* fallthrough */
  656. case 1:
  657. if (parent_sd->s_parent)
  658. ino = parent_sd->s_parent->s_ino;
  659. else
  660. ino = parent_sd->s_ino;
  661. if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
  662. break;
  663. filp->f_pos++;
  664. i++;
  665. /* fallthrough */
  666. default:
  667. pos = &parent_sd->s_children;
  668. while (*pos != cursor)
  669. pos = &(*pos)->s_sibling;
  670. /* unlink cursor */
  671. *pos = cursor->s_sibling;
  672. if (filp->f_pos == 2)
  673. pos = &parent_sd->s_children;
  674. for ( ; *pos; pos = &(*pos)->s_sibling) {
  675. struct sysfs_dirent *next = *pos;
  676. const char * name;
  677. int len;
  678. if (!sysfs_type(next))
  679. continue;
  680. name = next->s_name;
  681. len = strlen(name);
  682. ino = next->s_ino;
  683. if (filldir(dirent, name, len, filp->f_pos, ino,
  684. dt_type(next)) < 0)
  685. break;
  686. filp->f_pos++;
  687. }
  688. /* put cursor back in */
  689. cursor->s_sibling = *pos;
  690. *pos = cursor;
  691. }
  692. return 0;
  693. }
  694. static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
  695. {
  696. struct dentry * dentry = file->f_path.dentry;
  697. mutex_lock(&dentry->d_inode->i_mutex);
  698. switch (origin) {
  699. case 1:
  700. offset += file->f_pos;
  701. case 0:
  702. if (offset >= 0)
  703. break;
  704. default:
  705. mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
  706. return -EINVAL;
  707. }
  708. if (offset != file->f_pos) {
  709. file->f_pos = offset;
  710. if (file->f_pos >= 2) {
  711. struct sysfs_dirent *sd = dentry->d_fsdata;
  712. struct sysfs_dirent *cursor = file->private_data;
  713. struct sysfs_dirent **pos;
  714. loff_t n = file->f_pos - 2;
  715. sysfs_unlink_sibling(cursor);
  716. pos = &sd->s_children;
  717. while (n && *pos) {
  718. struct sysfs_dirent *next = *pos;
  719. if (sysfs_type(next))
  720. n--;
  721. pos = &(*pos)->s_sibling;
  722. }
  723. cursor->s_sibling = *pos;
  724. *pos = cursor;
  725. }
  726. }
  727. mutex_unlock(&dentry->d_inode->i_mutex);
  728. return offset;
  729. }
  730. /**
  731. * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
  732. * @kobj: object we're creating shadow of.
  733. */
  734. int sysfs_make_shadowed_dir(struct kobject *kobj,
  735. void * (*follow_link)(struct dentry *, struct nameidata *))
  736. {
  737. struct inode *inode;
  738. struct inode_operations *i_op;
  739. inode = kobj->dentry->d_inode;
  740. if (inode->i_op != &sysfs_dir_inode_operations)
  741. return -EINVAL;
  742. i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
  743. if (!i_op)
  744. return -ENOMEM;
  745. memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
  746. i_op->follow_link = follow_link;
  747. /* Locking of inode->i_op?
  748. * Since setting i_op is a single word write and they
  749. * are atomic we should be ok here.
  750. */
  751. inode->i_op = i_op;
  752. return 0;
  753. }
  754. /**
  755. * sysfs_create_shadow_dir - create a shadow directory for an object.
  756. * @kobj: object we're creating directory for.
  757. *
  758. * sysfs_make_shadowed_dir must already have been called on this
  759. * directory.
  760. */
  761. struct dentry *sysfs_create_shadow_dir(struct kobject *kobj)
  762. {
  763. struct dentry *dir = kobj->dentry;
  764. struct inode *inode = dir->d_inode;
  765. struct dentry *parent = dir->d_parent;
  766. struct sysfs_dirent *parent_sd = parent->d_fsdata;
  767. struct dentry *shadow;
  768. struct sysfs_dirent *sd;
  769. shadow = ERR_PTR(-EINVAL);
  770. if (!sysfs_is_shadowed_inode(inode))
  771. goto out;
  772. shadow = d_alloc(parent, &dir->d_name);
  773. if (!shadow)
  774. goto nomem;
  775. sd = sysfs_new_dirent("_SHADOW_", inode->i_mode, SYSFS_DIR);
  776. if (!sd)
  777. goto nomem;
  778. sd->s_elem.dir.kobj = kobj;
  779. /* point to parent_sd but don't attach to it */
  780. sd->s_parent = sysfs_get(parent_sd);
  781. sysfs_attach_dirent(sd, NULL, shadow);
  782. d_instantiate(shadow, igrab(inode));
  783. inc_nlink(inode);
  784. inc_nlink(parent->d_inode);
  785. dget(shadow); /* Extra count - pin the dentry in core */
  786. out:
  787. return shadow;
  788. nomem:
  789. dput(shadow);
  790. shadow = ERR_PTR(-ENOMEM);
  791. goto out;
  792. }
  793. /**
  794. * sysfs_remove_shadow_dir - remove an object's directory.
  795. * @shadow: dentry of shadow directory
  796. *
  797. * The only thing special about this is that we remove any files in
  798. * the directory before we remove the directory, and we've inlined
  799. * what used to be sysfs_rmdir() below, instead of calling separately.
  800. */
  801. void sysfs_remove_shadow_dir(struct dentry *shadow)
  802. {
  803. __sysfs_remove_dir(shadow);
  804. }
  805. const struct file_operations sysfs_dir_operations = {
  806. .open = sysfs_dir_open,
  807. .release = sysfs_dir_close,
  808. .llseek = sysfs_dir_lseek,
  809. .read = generic_read_dir,
  810. .readdir = sysfs_readdir,
  811. };