dir.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  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. DEFINE_MUTEX(sysfs_mutex);
  15. spinlock_t 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_link_sibling - link sysfs_dirent into sibling list
  20. * @sd: sysfs_dirent of interest
  21. *
  22. * Link @sd into its sibling list which starts from
  23. * sd->s_parent->s_children.
  24. *
  25. * Locking:
  26. * mutex_lock(sysfs_mutex)
  27. */
  28. void sysfs_link_sibling(struct sysfs_dirent *sd)
  29. {
  30. struct sysfs_dirent *parent_sd = sd->s_parent;
  31. BUG_ON(sd->s_sibling);
  32. sd->s_sibling = parent_sd->s_children;
  33. parent_sd->s_children = sd;
  34. }
  35. /**
  36. * sysfs_unlink_sibling - unlink sysfs_dirent from sibling list
  37. * @sd: sysfs_dirent of interest
  38. *
  39. * Unlink @sd from its sibling list which starts from
  40. * sd->s_parent->s_children.
  41. *
  42. * Locking:
  43. * mutex_lock(sysfs_mutex)
  44. */
  45. void sysfs_unlink_sibling(struct sysfs_dirent *sd)
  46. {
  47. struct sysfs_dirent **pos;
  48. for (pos = &sd->s_parent->s_children; *pos; pos = &(*pos)->s_sibling) {
  49. if (*pos == sd) {
  50. *pos = sd->s_sibling;
  51. sd->s_sibling = NULL;
  52. break;
  53. }
  54. }
  55. }
  56. /**
  57. * sysfs_get_active - get an active reference to sysfs_dirent
  58. * @sd: sysfs_dirent to get an active reference to
  59. *
  60. * Get an active reference of @sd. This function is noop if @sd
  61. * is NULL.
  62. *
  63. * RETURNS:
  64. * Pointer to @sd on success, NULL on failure.
  65. */
  66. struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
  67. {
  68. if (unlikely(!sd))
  69. return NULL;
  70. while (1) {
  71. int v, t;
  72. v = atomic_read(&sd->s_active);
  73. if (unlikely(v < 0))
  74. return NULL;
  75. t = atomic_cmpxchg(&sd->s_active, v, v + 1);
  76. if (likely(t == v))
  77. return sd;
  78. if (t < 0)
  79. return NULL;
  80. cpu_relax();
  81. }
  82. }
  83. /**
  84. * sysfs_put_active - put an active reference to sysfs_dirent
  85. * @sd: sysfs_dirent to put an active reference to
  86. *
  87. * Put an active reference to @sd. This function is noop if @sd
  88. * is NULL.
  89. */
  90. void sysfs_put_active(struct sysfs_dirent *sd)
  91. {
  92. struct completion *cmpl;
  93. int v;
  94. if (unlikely(!sd))
  95. return;
  96. v = atomic_dec_return(&sd->s_active);
  97. if (likely(v != SD_DEACTIVATED_BIAS))
  98. return;
  99. /* atomic_dec_return() is a mb(), we'll always see the updated
  100. * sd->s_sibling.
  101. */
  102. cmpl = (void *)sd->s_sibling;
  103. complete(cmpl);
  104. }
  105. /**
  106. * sysfs_get_active_two - get active references to sysfs_dirent and parent
  107. * @sd: sysfs_dirent of interest
  108. *
  109. * Get active reference to @sd and its parent. Parent's active
  110. * reference is grabbed first. This function is noop if @sd is
  111. * NULL.
  112. *
  113. * RETURNS:
  114. * Pointer to @sd on success, NULL on failure.
  115. */
  116. struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd)
  117. {
  118. if (sd) {
  119. if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent)))
  120. return NULL;
  121. if (unlikely(!sysfs_get_active(sd))) {
  122. sysfs_put_active(sd->s_parent);
  123. return NULL;
  124. }
  125. }
  126. return sd;
  127. }
  128. /**
  129. * sysfs_put_active_two - put active references to sysfs_dirent and parent
  130. * @sd: sysfs_dirent of interest
  131. *
  132. * Put active references to @sd and its parent. This function is
  133. * noop if @sd is NULL.
  134. */
  135. void sysfs_put_active_two(struct sysfs_dirent *sd)
  136. {
  137. if (sd) {
  138. sysfs_put_active(sd);
  139. sysfs_put_active(sd->s_parent);
  140. }
  141. }
  142. /**
  143. * sysfs_deactivate - deactivate sysfs_dirent
  144. * @sd: sysfs_dirent to deactivate
  145. *
  146. * Deny new active references and drain existing ones.
  147. */
  148. static void sysfs_deactivate(struct sysfs_dirent *sd)
  149. {
  150. DECLARE_COMPLETION_ONSTACK(wait);
  151. int v;
  152. BUG_ON(sd->s_sibling || !(sd->s_flags & SYSFS_FLAG_REMOVED));
  153. sd->s_sibling = (void *)&wait;
  154. /* atomic_add_return() is a mb(), put_active() will always see
  155. * the updated sd->s_sibling.
  156. */
  157. v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
  158. if (v != SD_DEACTIVATED_BIAS)
  159. wait_for_completion(&wait);
  160. sd->s_sibling = NULL;
  161. }
  162. static int sysfs_alloc_ino(ino_t *pino)
  163. {
  164. int ino, rc;
  165. retry:
  166. spin_lock(&sysfs_ino_lock);
  167. rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
  168. spin_unlock(&sysfs_ino_lock);
  169. if (rc == -EAGAIN) {
  170. if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
  171. goto retry;
  172. rc = -ENOMEM;
  173. }
  174. *pino = ino;
  175. return rc;
  176. }
  177. static void sysfs_free_ino(ino_t ino)
  178. {
  179. spin_lock(&sysfs_ino_lock);
  180. ida_remove(&sysfs_ino_ida, ino);
  181. spin_unlock(&sysfs_ino_lock);
  182. }
  183. void release_sysfs_dirent(struct sysfs_dirent * sd)
  184. {
  185. struct sysfs_dirent *parent_sd;
  186. repeat:
  187. /* Moving/renaming is always done while holding reference.
  188. * sd->s_parent won't change beneath us.
  189. */
  190. parent_sd = sd->s_parent;
  191. if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
  192. sysfs_put(sd->s_elem.symlink.target_sd);
  193. if (sysfs_type(sd) & SYSFS_COPY_NAME)
  194. kfree(sd->s_name);
  195. kfree(sd->s_iattr);
  196. sysfs_free_ino(sd->s_ino);
  197. kmem_cache_free(sysfs_dir_cachep, sd);
  198. sd = parent_sd;
  199. if (sd && atomic_dec_and_test(&sd->s_count))
  200. goto repeat;
  201. }
  202. static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
  203. {
  204. struct sysfs_dirent * sd = dentry->d_fsdata;
  205. if (sd) {
  206. /* sd->s_dentry is protected with sysfs_assoc_lock.
  207. * This allows sysfs_drop_dentry() to dereference it.
  208. */
  209. spin_lock(&sysfs_assoc_lock);
  210. /* The dentry might have been deleted or another
  211. * lookup could have happened updating sd->s_dentry to
  212. * point the new dentry. Ignore if it isn't pointing
  213. * to this dentry.
  214. */
  215. if (sd->s_dentry == dentry)
  216. sd->s_dentry = NULL;
  217. spin_unlock(&sysfs_assoc_lock);
  218. sysfs_put(sd);
  219. }
  220. iput(inode);
  221. }
  222. static struct dentry_operations sysfs_dentry_ops = {
  223. .d_iput = sysfs_d_iput,
  224. };
  225. struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
  226. {
  227. char *dup_name = NULL;
  228. struct sysfs_dirent *sd = NULL;
  229. if (type & SYSFS_COPY_NAME) {
  230. name = dup_name = kstrdup(name, GFP_KERNEL);
  231. if (!name)
  232. goto err_out;
  233. }
  234. sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
  235. if (!sd)
  236. goto err_out;
  237. if (sysfs_alloc_ino(&sd->s_ino))
  238. goto err_out;
  239. atomic_set(&sd->s_count, 1);
  240. atomic_set(&sd->s_active, 0);
  241. atomic_set(&sd->s_event, 1);
  242. sd->s_name = name;
  243. sd->s_mode = mode;
  244. sd->s_flags = type;
  245. return sd;
  246. err_out:
  247. kfree(dup_name);
  248. kmem_cache_free(sysfs_dir_cachep, sd);
  249. return NULL;
  250. }
  251. /**
  252. * sysfs_attach_dentry - associate sysfs_dirent with dentry
  253. * @sd: target sysfs_dirent
  254. * @dentry: dentry to associate
  255. *
  256. * Associate @sd with @dentry. This is protected by
  257. * sysfs_assoc_lock to avoid race with sysfs_d_iput().
  258. *
  259. * LOCKING:
  260. * mutex_lock(sysfs_mutex)
  261. */
  262. static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry)
  263. {
  264. dentry->d_op = &sysfs_dentry_ops;
  265. dentry->d_fsdata = sysfs_get(sd);
  266. /* protect sd->s_dentry against sysfs_d_iput */
  267. spin_lock(&sysfs_assoc_lock);
  268. sd->s_dentry = dentry;
  269. spin_unlock(&sysfs_assoc_lock);
  270. d_rehash(dentry);
  271. }
  272. static int sysfs_ilookup_test(struct inode *inode, void *arg)
  273. {
  274. struct sysfs_dirent *sd = arg;
  275. return inode->i_ino == sd->s_ino;
  276. }
  277. /**
  278. * sysfs_addrm_start - prepare for sysfs_dirent add/remove
  279. * @acxt: pointer to sysfs_addrm_cxt to be used
  280. * @parent_sd: parent sysfs_dirent
  281. *
  282. * This function is called when the caller is about to add or
  283. * remove sysfs_dirent under @parent_sd. This function acquires
  284. * sysfs_mutex, grabs inode for @parent_sd if available and lock
  285. * i_mutex of it. @acxt is used to keep and pass context to
  286. * other addrm functions.
  287. *
  288. * LOCKING:
  289. * Kernel thread context (may sleep). sysfs_mutex is locked on
  290. * return. i_mutex of parent inode is locked on return if
  291. * available.
  292. */
  293. void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt,
  294. struct sysfs_dirent *parent_sd)
  295. {
  296. struct inode *inode;
  297. memset(acxt, 0, sizeof(*acxt));
  298. acxt->parent_sd = parent_sd;
  299. /* Lookup parent inode. inode initialization and I_NEW
  300. * clearing are protected by sysfs_mutex. By grabbing it and
  301. * looking up with _nowait variant, inode state can be
  302. * determined reliably.
  303. */
  304. mutex_lock(&sysfs_mutex);
  305. inode = ilookup5_nowait(sysfs_sb, parent_sd->s_ino, sysfs_ilookup_test,
  306. parent_sd);
  307. if (inode && !(inode->i_state & I_NEW)) {
  308. /* parent inode available */
  309. acxt->parent_inode = inode;
  310. /* sysfs_mutex is below i_mutex in lock hierarchy.
  311. * First, trylock i_mutex. If fails, unlock
  312. * sysfs_mutex and lock them in order.
  313. */
  314. if (!mutex_trylock(&inode->i_mutex)) {
  315. mutex_unlock(&sysfs_mutex);
  316. mutex_lock(&inode->i_mutex);
  317. mutex_lock(&sysfs_mutex);
  318. }
  319. } else
  320. iput(inode);
  321. }
  322. /**
  323. * sysfs_add_one - add sysfs_dirent to parent
  324. * @acxt: addrm context to use
  325. * @sd: sysfs_dirent to be added
  326. *
  327. * Get @acxt->parent_sd and set sd->s_parent to it and increment
  328. * nlink of parent inode if @sd is a directory. @sd is NOT
  329. * linked into the children list of the parent. The caller
  330. * should invoke sysfs_link_sibling() after this function
  331. * completes if @sd needs to be on the children list.
  332. *
  333. * This function should be called between calls to
  334. * sysfs_addrm_start() and sysfs_addrm_finish() and should be
  335. * passed the same @acxt as passed to sysfs_addrm_start().
  336. *
  337. * LOCKING:
  338. * Determined by sysfs_addrm_start().
  339. */
  340. void sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
  341. {
  342. sd->s_parent = sysfs_get(acxt->parent_sd);
  343. if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
  344. inc_nlink(acxt->parent_inode);
  345. acxt->cnt++;
  346. }
  347. /**
  348. * sysfs_remove_one - remove sysfs_dirent from parent
  349. * @acxt: addrm context to use
  350. * @sd: sysfs_dirent to be added
  351. *
  352. * Mark @sd removed and drop nlink of parent inode if @sd is a
  353. * directory. @sd is NOT unlinked from the children list of the
  354. * parent. The caller is repsonsible for removing @sd from the
  355. * children list before calling this function.
  356. *
  357. * This function should be called between calls to
  358. * sysfs_addrm_start() and sysfs_addrm_finish() and should be
  359. * passed the same @acxt as passed to sysfs_addrm_start().
  360. *
  361. * LOCKING:
  362. * Determined by sysfs_addrm_start().
  363. */
  364. void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
  365. {
  366. BUG_ON(sd->s_sibling || (sd->s_flags & SYSFS_FLAG_REMOVED));
  367. sd->s_flags |= SYSFS_FLAG_REMOVED;
  368. sd->s_sibling = acxt->removed;
  369. acxt->removed = sd;
  370. if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
  371. drop_nlink(acxt->parent_inode);
  372. acxt->cnt++;
  373. }
  374. /**
  375. * sysfs_addrm_finish - finish up sysfs_dirent add/remove
  376. * @acxt: addrm context to finish up
  377. *
  378. * Finish up sysfs_dirent add/remove. Resources acquired by
  379. * sysfs_addrm_start() are released and removed sysfs_dirents are
  380. * cleaned up. Timestamps on the parent inode are updated.
  381. *
  382. * LOCKING:
  383. * All mutexes acquired by sysfs_addrm_start() are released.
  384. *
  385. * RETURNS:
  386. * Number of added/removed sysfs_dirents since sysfs_addrm_start().
  387. */
  388. int sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
  389. {
  390. /* release resources acquired by sysfs_addrm_start() */
  391. mutex_unlock(&sysfs_mutex);
  392. if (acxt->parent_inode) {
  393. struct inode *inode = acxt->parent_inode;
  394. /* if added/removed, update timestamps on the parent */
  395. if (acxt->cnt)
  396. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  397. mutex_unlock(&inode->i_mutex);
  398. iput(inode);
  399. }
  400. /* kill removed sysfs_dirents */
  401. while (acxt->removed) {
  402. struct sysfs_dirent *sd = acxt->removed;
  403. acxt->removed = sd->s_sibling;
  404. sd->s_sibling = NULL;
  405. sysfs_drop_dentry(sd);
  406. sysfs_deactivate(sd);
  407. sysfs_put(sd);
  408. }
  409. return acxt->cnt;
  410. }
  411. /**
  412. * sysfs_find_dirent - find sysfs_dirent with the given name
  413. * @parent_sd: sysfs_dirent to search under
  414. * @name: name to look for
  415. *
  416. * Look for sysfs_dirent with name @name under @parent_sd.
  417. *
  418. * LOCKING:
  419. * mutex_lock(sysfs_mutex)
  420. *
  421. * RETURNS:
  422. * Pointer to sysfs_dirent if found, NULL if not.
  423. */
  424. struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
  425. const unsigned char *name)
  426. {
  427. struct sysfs_dirent *sd;
  428. for (sd = parent_sd->s_children; sd; sd = sd->s_sibling)
  429. if (sysfs_type(sd) && !strcmp(sd->s_name, name))
  430. return sd;
  431. return NULL;
  432. }
  433. /**
  434. * sysfs_get_dirent - find and get sysfs_dirent with the given name
  435. * @parent_sd: sysfs_dirent to search under
  436. * @name: name to look for
  437. *
  438. * Look for sysfs_dirent with name @name under @parent_sd and get
  439. * it if found.
  440. *
  441. * LOCKING:
  442. * Kernel thread context (may sleep). Grabs sysfs_mutex.
  443. *
  444. * RETURNS:
  445. * Pointer to sysfs_dirent if found, NULL if not.
  446. */
  447. struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
  448. const unsigned char *name)
  449. {
  450. struct sysfs_dirent *sd;
  451. mutex_lock(&sysfs_mutex);
  452. sd = sysfs_find_dirent(parent_sd, name);
  453. sysfs_get(sd);
  454. mutex_unlock(&sysfs_mutex);
  455. return sd;
  456. }
  457. static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
  458. const char *name, struct sysfs_dirent **p_sd)
  459. {
  460. struct dentry *parent = parent_sd->s_dentry;
  461. struct sysfs_addrm_cxt acxt;
  462. int error;
  463. umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
  464. struct dentry *dentry;
  465. struct inode *inode;
  466. struct sysfs_dirent *sd;
  467. sysfs_addrm_start(&acxt, parent_sd);
  468. /* allocate */
  469. dentry = lookup_one_len(name, parent, strlen(name));
  470. if (IS_ERR(dentry)) {
  471. error = PTR_ERR(dentry);
  472. goto out_finish;
  473. }
  474. error = -EEXIST;
  475. if (dentry->d_inode)
  476. goto out_dput;
  477. error = -ENOMEM;
  478. sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
  479. if (!sd)
  480. goto out_drop;
  481. sd->s_elem.dir.kobj = kobj;
  482. inode = sysfs_get_inode(sd);
  483. if (!inode)
  484. goto out_sput;
  485. if (inode->i_state & I_NEW) {
  486. inode->i_op = &sysfs_dir_inode_operations;
  487. inode->i_fop = &sysfs_dir_operations;
  488. /* directory inodes start off with i_nlink == 2 (for ".") */
  489. inc_nlink(inode);
  490. }
  491. /* link in */
  492. error = -EEXIST;
  493. if (sysfs_find_dirent(parent_sd, name))
  494. goto out_iput;
  495. sysfs_add_one(&acxt, sd);
  496. sysfs_link_sibling(sd);
  497. sysfs_instantiate(dentry, inode);
  498. sysfs_attach_dentry(sd, dentry);
  499. *p_sd = sd;
  500. error = 0;
  501. goto out_finish; /* pin directory dentry in core */
  502. out_iput:
  503. iput(inode);
  504. out_sput:
  505. sysfs_put(sd);
  506. out_drop:
  507. d_drop(dentry);
  508. out_dput:
  509. dput(dentry);
  510. out_finish:
  511. sysfs_addrm_finish(&acxt);
  512. return error;
  513. }
  514. int sysfs_create_subdir(struct kobject *kobj, const char *name,
  515. struct sysfs_dirent **p_sd)
  516. {
  517. return create_dir(kobj, kobj->sd, name, p_sd);
  518. }
  519. /**
  520. * sysfs_create_dir - create a directory for an object.
  521. * @kobj: object we're creating directory for.
  522. * @shadow_parent: parent object.
  523. */
  524. int sysfs_create_dir(struct kobject *kobj,
  525. struct sysfs_dirent *shadow_parent_sd)
  526. {
  527. struct sysfs_dirent *parent_sd, *sd;
  528. int error = 0;
  529. BUG_ON(!kobj);
  530. if (shadow_parent_sd)
  531. parent_sd = shadow_parent_sd;
  532. else if (kobj->parent)
  533. parent_sd = kobj->parent->sd;
  534. else if (sysfs_mount && sysfs_mount->mnt_sb)
  535. parent_sd = sysfs_mount->mnt_sb->s_root->d_fsdata;
  536. else
  537. return -EFAULT;
  538. error = create_dir(kobj, parent_sd, kobject_name(kobj), &sd);
  539. if (!error)
  540. kobj->sd = sd;
  541. return error;
  542. }
  543. static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
  544. struct nameidata *nd)
  545. {
  546. struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
  547. struct sysfs_dirent * sd;
  548. struct bin_attribute *bin_attr;
  549. struct inode *inode;
  550. int found = 0;
  551. for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) {
  552. if ((sysfs_type(sd) & SYSFS_NOT_PINNED) &&
  553. !strcmp(sd->s_name, dentry->d_name.name)) {
  554. found = 1;
  555. break;
  556. }
  557. }
  558. /* no such entry */
  559. if (!found)
  560. return NULL;
  561. /* attach dentry and inode */
  562. inode = sysfs_get_inode(sd);
  563. if (!inode)
  564. return ERR_PTR(-ENOMEM);
  565. mutex_lock(&sysfs_mutex);
  566. if (inode->i_state & I_NEW) {
  567. /* initialize inode according to type */
  568. switch (sysfs_type(sd)) {
  569. case SYSFS_KOBJ_ATTR:
  570. inode->i_size = PAGE_SIZE;
  571. inode->i_fop = &sysfs_file_operations;
  572. break;
  573. case SYSFS_KOBJ_BIN_ATTR:
  574. bin_attr = sd->s_elem.bin_attr.bin_attr;
  575. inode->i_size = bin_attr->size;
  576. inode->i_fop = &bin_fops;
  577. break;
  578. case SYSFS_KOBJ_LINK:
  579. inode->i_op = &sysfs_symlink_inode_operations;
  580. break;
  581. default:
  582. BUG();
  583. }
  584. }
  585. sysfs_instantiate(dentry, inode);
  586. sysfs_attach_dentry(sd, dentry);
  587. mutex_unlock(&sysfs_mutex);
  588. return NULL;
  589. }
  590. const struct inode_operations sysfs_dir_inode_operations = {
  591. .lookup = sysfs_lookup,
  592. .setattr = sysfs_setattr,
  593. };
  594. static void remove_dir(struct sysfs_dirent *sd)
  595. {
  596. struct sysfs_addrm_cxt acxt;
  597. sysfs_addrm_start(&acxt, sd->s_parent);
  598. sysfs_unlink_sibling(sd);
  599. sysfs_remove_one(&acxt, sd);
  600. sysfs_addrm_finish(&acxt);
  601. }
  602. void sysfs_remove_subdir(struct sysfs_dirent *sd)
  603. {
  604. remove_dir(sd);
  605. }
  606. static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
  607. {
  608. struct sysfs_addrm_cxt acxt;
  609. struct sysfs_dirent **pos;
  610. if (!dir_sd)
  611. return;
  612. pr_debug("sysfs %s: removing dir\n", dir_sd->s_name);
  613. sysfs_addrm_start(&acxt, dir_sd);
  614. pos = &dir_sd->s_children;
  615. while (*pos) {
  616. struct sysfs_dirent *sd = *pos;
  617. if (sysfs_type(sd) && (sysfs_type(sd) & SYSFS_NOT_PINNED)) {
  618. *pos = sd->s_sibling;
  619. sd->s_sibling = NULL;
  620. sysfs_remove_one(&acxt, sd);
  621. } else
  622. pos = &(*pos)->s_sibling;
  623. }
  624. sysfs_addrm_finish(&acxt);
  625. remove_dir(dir_sd);
  626. }
  627. /**
  628. * sysfs_remove_dir - remove an object's directory.
  629. * @kobj: object.
  630. *
  631. * The only thing special about this is that we remove any files in
  632. * the directory before we remove the directory, and we've inlined
  633. * what used to be sysfs_rmdir() below, instead of calling separately.
  634. */
  635. void sysfs_remove_dir(struct kobject * kobj)
  636. {
  637. struct sysfs_dirent *sd = kobj->sd;
  638. spin_lock(&sysfs_assoc_lock);
  639. kobj->sd = NULL;
  640. spin_unlock(&sysfs_assoc_lock);
  641. __sysfs_remove_dir(sd);
  642. }
  643. int sysfs_rename_dir(struct kobject *kobj, struct sysfs_dirent *new_parent_sd,
  644. const char *new_name)
  645. {
  646. struct sysfs_dirent *sd = kobj->sd;
  647. struct dentry *new_parent = new_parent_sd->s_dentry;
  648. struct dentry *new_dentry;
  649. char *dup_name;
  650. int error;
  651. if (!new_parent_sd)
  652. return -EFAULT;
  653. mutex_lock(&new_parent->d_inode->i_mutex);
  654. new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
  655. if (IS_ERR(new_dentry)) {
  656. error = PTR_ERR(new_dentry);
  657. goto out_unlock;
  658. }
  659. /* By allowing two different directories with the same
  660. * d_parent we allow this routine to move between different
  661. * shadows of the same directory
  662. */
  663. error = -EINVAL;
  664. if (sd->s_parent->s_dentry->d_inode != new_parent->d_inode ||
  665. new_dentry->d_parent->d_inode != new_parent->d_inode ||
  666. new_dentry == sd->s_dentry)
  667. goto out_dput;
  668. error = -EEXIST;
  669. if (new_dentry->d_inode)
  670. goto out_dput;
  671. /* rename kobject and sysfs_dirent */
  672. error = -ENOMEM;
  673. new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
  674. if (!new_name)
  675. goto out_drop;
  676. error = kobject_set_name(kobj, "%s", new_name);
  677. if (error)
  678. goto out_free;
  679. kfree(sd->s_name);
  680. sd->s_name = new_name;
  681. /* move under the new parent */
  682. d_add(new_dentry, NULL);
  683. d_move(sd->s_dentry, new_dentry);
  684. mutex_lock(&sysfs_mutex);
  685. sysfs_unlink_sibling(sd);
  686. sysfs_get(new_parent_sd);
  687. sysfs_put(sd->s_parent);
  688. sd->s_parent = new_parent_sd;
  689. sysfs_link_sibling(sd);
  690. mutex_unlock(&sysfs_mutex);
  691. error = 0;
  692. goto out_unlock;
  693. out_free:
  694. kfree(dup_name);
  695. out_drop:
  696. d_drop(new_dentry);
  697. out_dput:
  698. dput(new_dentry);
  699. out_unlock:
  700. mutex_unlock(&new_parent->d_inode->i_mutex);
  701. return error;
  702. }
  703. int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
  704. {
  705. struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
  706. struct sysfs_dirent *new_parent_sd, *sd;
  707. int error;
  708. old_parent_dentry = kobj->parent ?
  709. kobj->parent->sd->s_dentry : sysfs_mount->mnt_sb->s_root;
  710. new_parent_dentry = new_parent ?
  711. new_parent->sd->s_dentry : sysfs_mount->mnt_sb->s_root;
  712. if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
  713. return 0; /* nothing to move */
  714. again:
  715. mutex_lock(&old_parent_dentry->d_inode->i_mutex);
  716. if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
  717. mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
  718. goto again;
  719. }
  720. new_parent_sd = new_parent_dentry->d_fsdata;
  721. sd = kobj->sd;
  722. new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
  723. strlen(kobj->name));
  724. if (IS_ERR(new_dentry)) {
  725. error = PTR_ERR(new_dentry);
  726. goto out;
  727. } else
  728. error = 0;
  729. d_add(new_dentry, NULL);
  730. d_move(sd->s_dentry, new_dentry);
  731. dput(new_dentry);
  732. /* Remove from old parent's list and insert into new parent's list. */
  733. mutex_lock(&sysfs_mutex);
  734. sysfs_unlink_sibling(sd);
  735. sysfs_get(new_parent_sd);
  736. sysfs_put(sd->s_parent);
  737. sd->s_parent = new_parent_sd;
  738. sysfs_link_sibling(sd);
  739. mutex_unlock(&sysfs_mutex);
  740. out:
  741. mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
  742. mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
  743. return error;
  744. }
  745. static int sysfs_dir_open(struct inode *inode, struct file *file)
  746. {
  747. struct dentry * dentry = file->f_path.dentry;
  748. struct sysfs_dirent * parent_sd = dentry->d_fsdata;
  749. struct sysfs_dirent * sd;
  750. sd = sysfs_new_dirent("_DIR_", 0, 0);
  751. if (sd) {
  752. mutex_lock(&sysfs_mutex);
  753. sd->s_parent = sysfs_get(parent_sd);
  754. sysfs_link_sibling(sd);
  755. mutex_unlock(&sysfs_mutex);
  756. }
  757. file->private_data = sd;
  758. return sd ? 0 : -ENOMEM;
  759. }
  760. static int sysfs_dir_close(struct inode *inode, struct file *file)
  761. {
  762. struct sysfs_dirent * cursor = file->private_data;
  763. mutex_lock(&sysfs_mutex);
  764. sysfs_unlink_sibling(cursor);
  765. mutex_unlock(&sysfs_mutex);
  766. release_sysfs_dirent(cursor);
  767. return 0;
  768. }
  769. /* Relationship between s_mode and the DT_xxx types */
  770. static inline unsigned char dt_type(struct sysfs_dirent *sd)
  771. {
  772. return (sd->s_mode >> 12) & 15;
  773. }
  774. static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
  775. {
  776. struct dentry *dentry = filp->f_path.dentry;
  777. struct sysfs_dirent * parent_sd = dentry->d_fsdata;
  778. struct sysfs_dirent *cursor = filp->private_data;
  779. struct sysfs_dirent **pos;
  780. ino_t ino;
  781. int i = filp->f_pos;
  782. switch (i) {
  783. case 0:
  784. ino = parent_sd->s_ino;
  785. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  786. break;
  787. filp->f_pos++;
  788. i++;
  789. /* fallthrough */
  790. case 1:
  791. if (parent_sd->s_parent)
  792. ino = parent_sd->s_parent->s_ino;
  793. else
  794. ino = parent_sd->s_ino;
  795. if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
  796. break;
  797. filp->f_pos++;
  798. i++;
  799. /* fallthrough */
  800. default:
  801. mutex_lock(&sysfs_mutex);
  802. pos = &parent_sd->s_children;
  803. while (*pos != cursor)
  804. pos = &(*pos)->s_sibling;
  805. /* unlink cursor */
  806. *pos = cursor->s_sibling;
  807. if (filp->f_pos == 2)
  808. pos = &parent_sd->s_children;
  809. for ( ; *pos; pos = &(*pos)->s_sibling) {
  810. struct sysfs_dirent *next = *pos;
  811. const char * name;
  812. int len;
  813. if (!sysfs_type(next))
  814. continue;
  815. name = next->s_name;
  816. len = strlen(name);
  817. ino = next->s_ino;
  818. if (filldir(dirent, name, len, filp->f_pos, ino,
  819. dt_type(next)) < 0)
  820. break;
  821. filp->f_pos++;
  822. }
  823. /* put cursor back in */
  824. cursor->s_sibling = *pos;
  825. *pos = cursor;
  826. mutex_unlock(&sysfs_mutex);
  827. }
  828. return 0;
  829. }
  830. static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
  831. {
  832. struct dentry * dentry = file->f_path.dentry;
  833. switch (origin) {
  834. case 1:
  835. offset += file->f_pos;
  836. case 0:
  837. if (offset >= 0)
  838. break;
  839. default:
  840. return -EINVAL;
  841. }
  842. if (offset != file->f_pos) {
  843. mutex_lock(&sysfs_mutex);
  844. file->f_pos = offset;
  845. if (file->f_pos >= 2) {
  846. struct sysfs_dirent *sd = dentry->d_fsdata;
  847. struct sysfs_dirent *cursor = file->private_data;
  848. struct sysfs_dirent **pos;
  849. loff_t n = file->f_pos - 2;
  850. sysfs_unlink_sibling(cursor);
  851. pos = &sd->s_children;
  852. while (n && *pos) {
  853. struct sysfs_dirent *next = *pos;
  854. if (sysfs_type(next))
  855. n--;
  856. pos = &(*pos)->s_sibling;
  857. }
  858. cursor->s_sibling = *pos;
  859. *pos = cursor;
  860. }
  861. mutex_unlock(&sysfs_mutex);
  862. }
  863. return offset;
  864. }
  865. /**
  866. * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
  867. * @kobj: object we're creating shadow of.
  868. */
  869. int sysfs_make_shadowed_dir(struct kobject *kobj,
  870. void * (*follow_link)(struct dentry *, struct nameidata *))
  871. {
  872. struct inode *inode;
  873. struct inode_operations *i_op;
  874. inode = kobj->sd->s_dentry->d_inode;
  875. if (inode->i_op != &sysfs_dir_inode_operations)
  876. return -EINVAL;
  877. i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
  878. if (!i_op)
  879. return -ENOMEM;
  880. memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
  881. i_op->follow_link = follow_link;
  882. /* Locking of inode->i_op?
  883. * Since setting i_op is a single word write and they
  884. * are atomic we should be ok here.
  885. */
  886. inode->i_op = i_op;
  887. return 0;
  888. }
  889. /**
  890. * sysfs_create_shadow_dir - create a shadow directory for an object.
  891. * @kobj: object we're creating directory for.
  892. *
  893. * sysfs_make_shadowed_dir must already have been called on this
  894. * directory.
  895. */
  896. struct sysfs_dirent *sysfs_create_shadow_dir(struct kobject *kobj)
  897. {
  898. struct dentry *dir = kobj->sd->s_dentry;
  899. struct inode *inode = dir->d_inode;
  900. struct dentry *parent = dir->d_parent;
  901. struct sysfs_dirent *parent_sd = parent->d_fsdata;
  902. struct dentry *shadow;
  903. struct sysfs_dirent *sd;
  904. struct sysfs_addrm_cxt acxt;
  905. sd = ERR_PTR(-EINVAL);
  906. if (!sysfs_is_shadowed_inode(inode))
  907. goto out;
  908. shadow = d_alloc(parent, &dir->d_name);
  909. if (!shadow)
  910. goto nomem;
  911. sd = sysfs_new_dirent("_SHADOW_", inode->i_mode, SYSFS_DIR);
  912. if (!sd)
  913. goto nomem;
  914. sd->s_elem.dir.kobj = kobj;
  915. sysfs_addrm_start(&acxt, parent_sd);
  916. /* add but don't link into children list */
  917. sysfs_add_one(&acxt, sd);
  918. /* attach and instantiate dentry */
  919. sysfs_attach_dentry(sd, shadow);
  920. d_instantiate(shadow, igrab(inode));
  921. inc_nlink(inode); /* tj: synchronization? */
  922. sysfs_addrm_finish(&acxt);
  923. dget(shadow); /* Extra count - pin the dentry in core */
  924. out:
  925. return sd;
  926. nomem:
  927. dput(shadow);
  928. sd = ERR_PTR(-ENOMEM);
  929. goto out;
  930. }
  931. /**
  932. * sysfs_remove_shadow_dir - remove an object's directory.
  933. * @shadow_sd: sysfs_dirent of shadow directory
  934. *
  935. * The only thing special about this is that we remove any files in
  936. * the directory before we remove the directory, and we've inlined
  937. * what used to be sysfs_rmdir() below, instead of calling separately.
  938. */
  939. void sysfs_remove_shadow_dir(struct sysfs_dirent *shadow_sd)
  940. {
  941. __sysfs_remove_dir(shadow_sd);
  942. }
  943. const struct file_operations sysfs_dir_operations = {
  944. .open = sysfs_dir_open,
  945. .release = sysfs_dir_close,
  946. .llseek = sysfs_dir_lseek,
  947. .read = generic_read_dir,
  948. .readdir = sysfs_readdir,
  949. };