dir.c 30 KB

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