inode.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415
  1. /*
  2. * linux/fs/inode.c
  3. *
  4. * (C) 1997 Linus Torvalds
  5. */
  6. #include <linux/fs.h>
  7. #include <linux/mm.h>
  8. #include <linux/dcache.h>
  9. #include <linux/init.h>
  10. #include <linux/quotaops.h>
  11. #include <linux/slab.h>
  12. #include <linux/writeback.h>
  13. #include <linux/module.h>
  14. #include <linux/backing-dev.h>
  15. #include <linux/wait.h>
  16. #include <linux/hash.h>
  17. #include <linux/swap.h>
  18. #include <linux/security.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/cdev.h>
  21. #include <linux/bootmem.h>
  22. #include <linux/inotify.h>
  23. #include <linux/mount.h>
  24. /*
  25. * This is needed for the following functions:
  26. * - inode_has_buffers
  27. * - invalidate_inode_buffers
  28. * - invalidate_bdev
  29. *
  30. * FIXME: remove all knowledge of the buffer layer from this file
  31. */
  32. #include <linux/buffer_head.h>
  33. /*
  34. * New inode.c implementation.
  35. *
  36. * This implementation has the basic premise of trying
  37. * to be extremely low-overhead and SMP-safe, yet be
  38. * simple enough to be "obviously correct".
  39. *
  40. * Famous last words.
  41. */
  42. /* inode dynamic allocation 1999, Andrea Arcangeli <andrea@suse.de> */
  43. /* #define INODE_PARANOIA 1 */
  44. /* #define INODE_DEBUG 1 */
  45. /*
  46. * Inode lookup is no longer as critical as it used to be:
  47. * most of the lookups are going to be through the dcache.
  48. */
  49. #define I_HASHBITS i_hash_shift
  50. #define I_HASHMASK i_hash_mask
  51. static unsigned int i_hash_mask __read_mostly;
  52. static unsigned int i_hash_shift __read_mostly;
  53. /*
  54. * Each inode can be on two separate lists. One is
  55. * the hash list of the inode, used for lookups. The
  56. * other linked list is the "type" list:
  57. * "in_use" - valid inode, i_count > 0, i_nlink > 0
  58. * "dirty" - as "in_use" but also dirty
  59. * "unused" - valid inode, i_count = 0
  60. *
  61. * A "dirty" list is maintained for each super block,
  62. * allowing for low-overhead inode sync() operations.
  63. */
  64. LIST_HEAD(inode_in_use);
  65. LIST_HEAD(inode_unused);
  66. static struct hlist_head *inode_hashtable __read_mostly;
  67. /*
  68. * A simple spinlock to protect the list manipulations.
  69. *
  70. * NOTE! You also have to own the lock if you change
  71. * the i_state of an inode while it is in use..
  72. */
  73. DEFINE_SPINLOCK(inode_lock);
  74. /*
  75. * iprune_mutex provides exclusion between the kswapd or try_to_free_pages
  76. * icache shrinking path, and the umount path. Without this exclusion,
  77. * by the time prune_icache calls iput for the inode whose pages it has
  78. * been invalidating, or by the time it calls clear_inode & destroy_inode
  79. * from its final dispose_list, the struct super_block they refer to
  80. * (for inode->i_sb->s_op) may already have been freed and reused.
  81. */
  82. static DEFINE_MUTEX(iprune_mutex);
  83. /*
  84. * Statistics gathering..
  85. */
  86. struct inodes_stat_t inodes_stat;
  87. static kmem_cache_t * inode_cachep __read_mostly;
  88. static struct inode *alloc_inode(struct super_block *sb)
  89. {
  90. static const struct address_space_operations empty_aops;
  91. static struct inode_operations empty_iops;
  92. static const struct file_operations empty_fops;
  93. struct inode *inode;
  94. if (sb->s_op->alloc_inode)
  95. inode = sb->s_op->alloc_inode(sb);
  96. else
  97. inode = (struct inode *) kmem_cache_alloc(inode_cachep, SLAB_KERNEL);
  98. if (inode) {
  99. struct address_space * const mapping = &inode->i_data;
  100. inode->i_sb = sb;
  101. inode->i_blkbits = sb->s_blocksize_bits;
  102. inode->i_flags = 0;
  103. atomic_set(&inode->i_count, 1);
  104. inode->i_op = &empty_iops;
  105. inode->i_fop = &empty_fops;
  106. inode->i_nlink = 1;
  107. atomic_set(&inode->i_writecount, 0);
  108. inode->i_size = 0;
  109. inode->i_blocks = 0;
  110. inode->i_bytes = 0;
  111. inode->i_generation = 0;
  112. #ifdef CONFIG_QUOTA
  113. memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
  114. #endif
  115. inode->i_pipe = NULL;
  116. inode->i_bdev = NULL;
  117. inode->i_cdev = NULL;
  118. inode->i_rdev = 0;
  119. inode->i_security = NULL;
  120. inode->dirtied_when = 0;
  121. if (security_inode_alloc(inode)) {
  122. if (inode->i_sb->s_op->destroy_inode)
  123. inode->i_sb->s_op->destroy_inode(inode);
  124. else
  125. kmem_cache_free(inode_cachep, (inode));
  126. return NULL;
  127. }
  128. mapping->a_ops = &empty_aops;
  129. mapping->host = inode;
  130. mapping->flags = 0;
  131. mapping_set_gfp_mask(mapping, GFP_HIGHUSER);
  132. mapping->assoc_mapping = NULL;
  133. mapping->backing_dev_info = &default_backing_dev_info;
  134. /*
  135. * If the block_device provides a backing_dev_info for client
  136. * inodes then use that. Otherwise the inode share the bdev's
  137. * backing_dev_info.
  138. */
  139. if (sb->s_bdev) {
  140. struct backing_dev_info *bdi;
  141. bdi = sb->s_bdev->bd_inode_backing_dev_info;
  142. if (!bdi)
  143. bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
  144. mapping->backing_dev_info = bdi;
  145. }
  146. inode->i_private = 0;
  147. inode->i_mapping = mapping;
  148. }
  149. return inode;
  150. }
  151. void destroy_inode(struct inode *inode)
  152. {
  153. BUG_ON(inode_has_buffers(inode));
  154. security_inode_free(inode);
  155. if (inode->i_sb->s_op->destroy_inode)
  156. inode->i_sb->s_op->destroy_inode(inode);
  157. else
  158. kmem_cache_free(inode_cachep, (inode));
  159. }
  160. /*
  161. * These are initializations that only need to be done
  162. * once, because the fields are idempotent across use
  163. * of the inode, so let the slab aware of that.
  164. */
  165. void inode_init_once(struct inode *inode)
  166. {
  167. memset(inode, 0, sizeof(*inode));
  168. INIT_HLIST_NODE(&inode->i_hash);
  169. INIT_LIST_HEAD(&inode->i_dentry);
  170. INIT_LIST_HEAD(&inode->i_devices);
  171. mutex_init(&inode->i_mutex);
  172. init_rwsem(&inode->i_alloc_sem);
  173. INIT_RADIX_TREE(&inode->i_data.page_tree, GFP_ATOMIC);
  174. rwlock_init(&inode->i_data.tree_lock);
  175. spin_lock_init(&inode->i_data.i_mmap_lock);
  176. INIT_LIST_HEAD(&inode->i_data.private_list);
  177. spin_lock_init(&inode->i_data.private_lock);
  178. INIT_RAW_PRIO_TREE_ROOT(&inode->i_data.i_mmap);
  179. INIT_LIST_HEAD(&inode->i_data.i_mmap_nonlinear);
  180. spin_lock_init(&inode->i_lock);
  181. i_size_ordered_init(inode);
  182. #ifdef CONFIG_INOTIFY
  183. INIT_LIST_HEAD(&inode->inotify_watches);
  184. mutex_init(&inode->inotify_mutex);
  185. #endif
  186. }
  187. EXPORT_SYMBOL(inode_init_once);
  188. static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
  189. {
  190. struct inode * inode = (struct inode *) foo;
  191. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  192. SLAB_CTOR_CONSTRUCTOR)
  193. inode_init_once(inode);
  194. }
  195. /*
  196. * inode_lock must be held
  197. */
  198. void __iget(struct inode * inode)
  199. {
  200. if (atomic_read(&inode->i_count)) {
  201. atomic_inc(&inode->i_count);
  202. return;
  203. }
  204. atomic_inc(&inode->i_count);
  205. if (!(inode->i_state & (I_DIRTY|I_LOCK)))
  206. list_move(&inode->i_list, &inode_in_use);
  207. inodes_stat.nr_unused--;
  208. }
  209. /**
  210. * clear_inode - clear an inode
  211. * @inode: inode to clear
  212. *
  213. * This is called by the filesystem to tell us
  214. * that the inode is no longer useful. We just
  215. * terminate it with extreme prejudice.
  216. */
  217. void clear_inode(struct inode *inode)
  218. {
  219. might_sleep();
  220. invalidate_inode_buffers(inode);
  221. BUG_ON(inode->i_data.nrpages);
  222. BUG_ON(!(inode->i_state & I_FREEING));
  223. BUG_ON(inode->i_state & I_CLEAR);
  224. wait_on_inode(inode);
  225. DQUOT_DROP(inode);
  226. if (inode->i_sb && inode->i_sb->s_op->clear_inode)
  227. inode->i_sb->s_op->clear_inode(inode);
  228. if (S_ISBLK(inode->i_mode) && inode->i_bdev)
  229. bd_forget(inode);
  230. if (S_ISCHR(inode->i_mode) && inode->i_cdev)
  231. cd_forget(inode);
  232. inode->i_state = I_CLEAR;
  233. }
  234. EXPORT_SYMBOL(clear_inode);
  235. /*
  236. * dispose_list - dispose of the contents of a local list
  237. * @head: the head of the list to free
  238. *
  239. * Dispose-list gets a local list with local inodes in it, so it doesn't
  240. * need to worry about list corruption and SMP locks.
  241. */
  242. static void dispose_list(struct list_head *head)
  243. {
  244. int nr_disposed = 0;
  245. while (!list_empty(head)) {
  246. struct inode *inode;
  247. inode = list_entry(head->next, struct inode, i_list);
  248. list_del(&inode->i_list);
  249. if (inode->i_data.nrpages)
  250. truncate_inode_pages(&inode->i_data, 0);
  251. clear_inode(inode);
  252. spin_lock(&inode_lock);
  253. hlist_del_init(&inode->i_hash);
  254. list_del_init(&inode->i_sb_list);
  255. spin_unlock(&inode_lock);
  256. wake_up_inode(inode);
  257. destroy_inode(inode);
  258. nr_disposed++;
  259. }
  260. spin_lock(&inode_lock);
  261. inodes_stat.nr_inodes -= nr_disposed;
  262. spin_unlock(&inode_lock);
  263. }
  264. /*
  265. * Invalidate all inodes for a device.
  266. */
  267. static int invalidate_list(struct list_head *head, struct list_head *dispose)
  268. {
  269. struct list_head *next;
  270. int busy = 0, count = 0;
  271. next = head->next;
  272. for (;;) {
  273. struct list_head * tmp = next;
  274. struct inode * inode;
  275. /*
  276. * We can reschedule here without worrying about the list's
  277. * consistency because the per-sb list of inodes must not
  278. * change during umount anymore, and because iprune_mutex keeps
  279. * shrink_icache_memory() away.
  280. */
  281. cond_resched_lock(&inode_lock);
  282. next = next->next;
  283. if (tmp == head)
  284. break;
  285. inode = list_entry(tmp, struct inode, i_sb_list);
  286. invalidate_inode_buffers(inode);
  287. if (!atomic_read(&inode->i_count)) {
  288. list_move(&inode->i_list, dispose);
  289. inode->i_state |= I_FREEING;
  290. count++;
  291. continue;
  292. }
  293. busy = 1;
  294. }
  295. /* only unused inodes may be cached with i_count zero */
  296. inodes_stat.nr_unused -= count;
  297. return busy;
  298. }
  299. /**
  300. * invalidate_inodes - discard the inodes on a device
  301. * @sb: superblock
  302. *
  303. * Discard all of the inodes for a given superblock. If the discard
  304. * fails because there are busy inodes then a non zero value is returned.
  305. * If the discard is successful all the inodes have been discarded.
  306. */
  307. int invalidate_inodes(struct super_block * sb)
  308. {
  309. int busy;
  310. LIST_HEAD(throw_away);
  311. mutex_lock(&iprune_mutex);
  312. spin_lock(&inode_lock);
  313. inotify_unmount_inodes(&sb->s_inodes);
  314. busy = invalidate_list(&sb->s_inodes, &throw_away);
  315. spin_unlock(&inode_lock);
  316. dispose_list(&throw_away);
  317. mutex_unlock(&iprune_mutex);
  318. return busy;
  319. }
  320. EXPORT_SYMBOL(invalidate_inodes);
  321. int __invalidate_device(struct block_device *bdev)
  322. {
  323. struct super_block *sb = get_super(bdev);
  324. int res = 0;
  325. if (sb) {
  326. /*
  327. * no need to lock the super, get_super holds the
  328. * read mutex so the filesystem cannot go away
  329. * under us (->put_super runs with the write lock
  330. * hold).
  331. */
  332. shrink_dcache_sb(sb);
  333. res = invalidate_inodes(sb);
  334. drop_super(sb);
  335. }
  336. invalidate_bdev(bdev, 0);
  337. return res;
  338. }
  339. EXPORT_SYMBOL(__invalidate_device);
  340. static int can_unuse(struct inode *inode)
  341. {
  342. if (inode->i_state)
  343. return 0;
  344. if (inode_has_buffers(inode))
  345. return 0;
  346. if (atomic_read(&inode->i_count))
  347. return 0;
  348. if (inode->i_data.nrpages)
  349. return 0;
  350. return 1;
  351. }
  352. /*
  353. * Scan `goal' inodes on the unused list for freeable ones. They are moved to
  354. * a temporary list and then are freed outside inode_lock by dispose_list().
  355. *
  356. * Any inodes which are pinned purely because of attached pagecache have their
  357. * pagecache removed. We expect the final iput() on that inode to add it to
  358. * the front of the inode_unused list. So look for it there and if the
  359. * inode is still freeable, proceed. The right inode is found 99.9% of the
  360. * time in testing on a 4-way.
  361. *
  362. * If the inode has metadata buffers attached to mapping->private_list then
  363. * try to remove them.
  364. */
  365. static void prune_icache(int nr_to_scan)
  366. {
  367. LIST_HEAD(freeable);
  368. int nr_pruned = 0;
  369. int nr_scanned;
  370. unsigned long reap = 0;
  371. mutex_lock(&iprune_mutex);
  372. spin_lock(&inode_lock);
  373. for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {
  374. struct inode *inode;
  375. if (list_empty(&inode_unused))
  376. break;
  377. inode = list_entry(inode_unused.prev, struct inode, i_list);
  378. if (inode->i_state || atomic_read(&inode->i_count)) {
  379. list_move(&inode->i_list, &inode_unused);
  380. continue;
  381. }
  382. if (inode_has_buffers(inode) || inode->i_data.nrpages) {
  383. __iget(inode);
  384. spin_unlock(&inode_lock);
  385. if (remove_inode_buffers(inode))
  386. reap += invalidate_inode_pages(&inode->i_data);
  387. iput(inode);
  388. spin_lock(&inode_lock);
  389. if (inode != list_entry(inode_unused.next,
  390. struct inode, i_list))
  391. continue; /* wrong inode or list_empty */
  392. if (!can_unuse(inode))
  393. continue;
  394. }
  395. list_move(&inode->i_list, &freeable);
  396. inode->i_state |= I_FREEING;
  397. nr_pruned++;
  398. }
  399. inodes_stat.nr_unused -= nr_pruned;
  400. if (current_is_kswapd())
  401. __count_vm_events(KSWAPD_INODESTEAL, reap);
  402. else
  403. __count_vm_events(PGINODESTEAL, reap);
  404. spin_unlock(&inode_lock);
  405. dispose_list(&freeable);
  406. mutex_unlock(&iprune_mutex);
  407. }
  408. /*
  409. * shrink_icache_memory() will attempt to reclaim some unused inodes. Here,
  410. * "unused" means that no dentries are referring to the inodes: the files are
  411. * not open and the dcache references to those inodes have already been
  412. * reclaimed.
  413. *
  414. * This function is passed the number of inodes to scan, and it returns the
  415. * total number of remaining possibly-reclaimable inodes.
  416. */
  417. static int shrink_icache_memory(int nr, gfp_t gfp_mask)
  418. {
  419. if (nr) {
  420. /*
  421. * Nasty deadlock avoidance. We may hold various FS locks,
  422. * and we don't want to recurse into the FS that called us
  423. * in clear_inode() and friends..
  424. */
  425. if (!(gfp_mask & __GFP_FS))
  426. return -1;
  427. prune_icache(nr);
  428. }
  429. return (inodes_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
  430. }
  431. static void __wait_on_freeing_inode(struct inode *inode);
  432. /*
  433. * Called with the inode lock held.
  434. * NOTE: we are not increasing the inode-refcount, you must call __iget()
  435. * by hand after calling find_inode now! This simplifies iunique and won't
  436. * add any additional branch in the common code.
  437. */
  438. static struct inode * find_inode(struct super_block * sb, struct hlist_head *head, int (*test)(struct inode *, void *), void *data)
  439. {
  440. struct hlist_node *node;
  441. struct inode * inode = NULL;
  442. repeat:
  443. hlist_for_each (node, head) {
  444. inode = hlist_entry(node, struct inode, i_hash);
  445. if (inode->i_sb != sb)
  446. continue;
  447. if (!test(inode, data))
  448. continue;
  449. if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) {
  450. __wait_on_freeing_inode(inode);
  451. goto repeat;
  452. }
  453. break;
  454. }
  455. return node ? inode : NULL;
  456. }
  457. /*
  458. * find_inode_fast is the fast path version of find_inode, see the comment at
  459. * iget_locked for details.
  460. */
  461. static struct inode * find_inode_fast(struct super_block * sb, struct hlist_head *head, unsigned long ino)
  462. {
  463. struct hlist_node *node;
  464. struct inode * inode = NULL;
  465. repeat:
  466. hlist_for_each (node, head) {
  467. inode = hlist_entry(node, struct inode, i_hash);
  468. if (inode->i_ino != ino)
  469. continue;
  470. if (inode->i_sb != sb)
  471. continue;
  472. if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) {
  473. __wait_on_freeing_inode(inode);
  474. goto repeat;
  475. }
  476. break;
  477. }
  478. return node ? inode : NULL;
  479. }
  480. /**
  481. * new_inode - obtain an inode
  482. * @sb: superblock
  483. *
  484. * Allocates a new inode for given superblock.
  485. */
  486. struct inode *new_inode(struct super_block *sb)
  487. {
  488. static unsigned long last_ino;
  489. struct inode * inode;
  490. spin_lock_prefetch(&inode_lock);
  491. inode = alloc_inode(sb);
  492. if (inode) {
  493. spin_lock(&inode_lock);
  494. inodes_stat.nr_inodes++;
  495. list_add(&inode->i_list, &inode_in_use);
  496. list_add(&inode->i_sb_list, &sb->s_inodes);
  497. inode->i_ino = ++last_ino;
  498. inode->i_state = 0;
  499. spin_unlock(&inode_lock);
  500. }
  501. return inode;
  502. }
  503. EXPORT_SYMBOL(new_inode);
  504. void unlock_new_inode(struct inode *inode)
  505. {
  506. /*
  507. * This is special! We do not need the spinlock
  508. * when clearing I_LOCK, because we're guaranteed
  509. * that nobody else tries to do anything about the
  510. * state of the inode when it is locked, as we
  511. * just created it (so there can be no old holders
  512. * that haven't tested I_LOCK).
  513. */
  514. inode->i_state &= ~(I_LOCK|I_NEW);
  515. wake_up_inode(inode);
  516. }
  517. EXPORT_SYMBOL(unlock_new_inode);
  518. /*
  519. * This is called without the inode lock held.. Be careful.
  520. *
  521. * We no longer cache the sb_flags in i_flags - see fs.h
  522. * -- rmk@arm.uk.linux.org
  523. */
  524. static struct inode * get_new_inode(struct super_block *sb, struct hlist_head *head, int (*test)(struct inode *, void *), int (*set)(struct inode *, void *), void *data)
  525. {
  526. struct inode * inode;
  527. inode = alloc_inode(sb);
  528. if (inode) {
  529. struct inode * old;
  530. spin_lock(&inode_lock);
  531. /* We released the lock, so.. */
  532. old = find_inode(sb, head, test, data);
  533. if (!old) {
  534. if (set(inode, data))
  535. goto set_failed;
  536. inodes_stat.nr_inodes++;
  537. list_add(&inode->i_list, &inode_in_use);
  538. list_add(&inode->i_sb_list, &sb->s_inodes);
  539. hlist_add_head(&inode->i_hash, head);
  540. inode->i_state = I_LOCK|I_NEW;
  541. spin_unlock(&inode_lock);
  542. /* Return the locked inode with I_NEW set, the
  543. * caller is responsible for filling in the contents
  544. */
  545. return inode;
  546. }
  547. /*
  548. * Uhhuh, somebody else created the same inode under
  549. * us. Use the old inode instead of the one we just
  550. * allocated.
  551. */
  552. __iget(old);
  553. spin_unlock(&inode_lock);
  554. destroy_inode(inode);
  555. inode = old;
  556. wait_on_inode(inode);
  557. }
  558. return inode;
  559. set_failed:
  560. spin_unlock(&inode_lock);
  561. destroy_inode(inode);
  562. return NULL;
  563. }
  564. /*
  565. * get_new_inode_fast is the fast path version of get_new_inode, see the
  566. * comment at iget_locked for details.
  567. */
  568. static struct inode * get_new_inode_fast(struct super_block *sb, struct hlist_head *head, unsigned long ino)
  569. {
  570. struct inode * inode;
  571. inode = alloc_inode(sb);
  572. if (inode) {
  573. struct inode * old;
  574. spin_lock(&inode_lock);
  575. /* We released the lock, so.. */
  576. old = find_inode_fast(sb, head, ino);
  577. if (!old) {
  578. inode->i_ino = ino;
  579. inodes_stat.nr_inodes++;
  580. list_add(&inode->i_list, &inode_in_use);
  581. list_add(&inode->i_sb_list, &sb->s_inodes);
  582. hlist_add_head(&inode->i_hash, head);
  583. inode->i_state = I_LOCK|I_NEW;
  584. spin_unlock(&inode_lock);
  585. /* Return the locked inode with I_NEW set, the
  586. * caller is responsible for filling in the contents
  587. */
  588. return inode;
  589. }
  590. /*
  591. * Uhhuh, somebody else created the same inode under
  592. * us. Use the old inode instead of the one we just
  593. * allocated.
  594. */
  595. __iget(old);
  596. spin_unlock(&inode_lock);
  597. destroy_inode(inode);
  598. inode = old;
  599. wait_on_inode(inode);
  600. }
  601. return inode;
  602. }
  603. static inline unsigned long hash(struct super_block *sb, unsigned long hashval)
  604. {
  605. unsigned long tmp;
  606. tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
  607. L1_CACHE_BYTES;
  608. tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> I_HASHBITS);
  609. return tmp & I_HASHMASK;
  610. }
  611. /**
  612. * iunique - get a unique inode number
  613. * @sb: superblock
  614. * @max_reserved: highest reserved inode number
  615. *
  616. * Obtain an inode number that is unique on the system for a given
  617. * superblock. This is used by file systems that have no natural
  618. * permanent inode numbering system. An inode number is returned that
  619. * is higher than the reserved limit but unique.
  620. *
  621. * BUGS:
  622. * With a large number of inodes live on the file system this function
  623. * currently becomes quite slow.
  624. */
  625. ino_t iunique(struct super_block *sb, ino_t max_reserved)
  626. {
  627. static ino_t counter;
  628. struct inode *inode;
  629. struct hlist_head * head;
  630. ino_t res;
  631. spin_lock(&inode_lock);
  632. retry:
  633. if (counter > max_reserved) {
  634. head = inode_hashtable + hash(sb,counter);
  635. res = counter++;
  636. inode = find_inode_fast(sb, head, res);
  637. if (!inode) {
  638. spin_unlock(&inode_lock);
  639. return res;
  640. }
  641. } else {
  642. counter = max_reserved + 1;
  643. }
  644. goto retry;
  645. }
  646. EXPORT_SYMBOL(iunique);
  647. struct inode *igrab(struct inode *inode)
  648. {
  649. spin_lock(&inode_lock);
  650. if (!(inode->i_state & (I_FREEING|I_WILL_FREE)))
  651. __iget(inode);
  652. else
  653. /*
  654. * Handle the case where s_op->clear_inode is not been
  655. * called yet, and somebody is calling igrab
  656. * while the inode is getting freed.
  657. */
  658. inode = NULL;
  659. spin_unlock(&inode_lock);
  660. return inode;
  661. }
  662. EXPORT_SYMBOL(igrab);
  663. /**
  664. * ifind - internal function, you want ilookup5() or iget5().
  665. * @sb: super block of file system to search
  666. * @head: the head of the list to search
  667. * @test: callback used for comparisons between inodes
  668. * @data: opaque data pointer to pass to @test
  669. * @wait: if true wait for the inode to be unlocked, if false do not
  670. *
  671. * ifind() searches for the inode specified by @data in the inode
  672. * cache. This is a generalized version of ifind_fast() for file systems where
  673. * the inode number is not sufficient for unique identification of an inode.
  674. *
  675. * If the inode is in the cache, the inode is returned with an incremented
  676. * reference count.
  677. *
  678. * Otherwise NULL is returned.
  679. *
  680. * Note, @test is called with the inode_lock held, so can't sleep.
  681. */
  682. static struct inode *ifind(struct super_block *sb,
  683. struct hlist_head *head, int (*test)(struct inode *, void *),
  684. void *data, const int wait)
  685. {
  686. struct inode *inode;
  687. spin_lock(&inode_lock);
  688. inode = find_inode(sb, head, test, data);
  689. if (inode) {
  690. __iget(inode);
  691. spin_unlock(&inode_lock);
  692. if (likely(wait))
  693. wait_on_inode(inode);
  694. return inode;
  695. }
  696. spin_unlock(&inode_lock);
  697. return NULL;
  698. }
  699. /**
  700. * ifind_fast - internal function, you want ilookup() or iget().
  701. * @sb: super block of file system to search
  702. * @head: head of the list to search
  703. * @ino: inode number to search for
  704. *
  705. * ifind_fast() searches for the inode @ino in the inode cache. This is for
  706. * file systems where the inode number is sufficient for unique identification
  707. * of an inode.
  708. *
  709. * If the inode is in the cache, the inode is returned with an incremented
  710. * reference count.
  711. *
  712. * Otherwise NULL is returned.
  713. */
  714. static struct inode *ifind_fast(struct super_block *sb,
  715. struct hlist_head *head, unsigned long ino)
  716. {
  717. struct inode *inode;
  718. spin_lock(&inode_lock);
  719. inode = find_inode_fast(sb, head, ino);
  720. if (inode) {
  721. __iget(inode);
  722. spin_unlock(&inode_lock);
  723. wait_on_inode(inode);
  724. return inode;
  725. }
  726. spin_unlock(&inode_lock);
  727. return NULL;
  728. }
  729. /**
  730. * ilookup5_nowait - search for an inode in the inode cache
  731. * @sb: super block of file system to search
  732. * @hashval: hash value (usually inode number) to search for
  733. * @test: callback used for comparisons between inodes
  734. * @data: opaque data pointer to pass to @test
  735. *
  736. * ilookup5() uses ifind() to search for the inode specified by @hashval and
  737. * @data in the inode cache. This is a generalized version of ilookup() for
  738. * file systems where the inode number is not sufficient for unique
  739. * identification of an inode.
  740. *
  741. * If the inode is in the cache, the inode is returned with an incremented
  742. * reference count. Note, the inode lock is not waited upon so you have to be
  743. * very careful what you do with the returned inode. You probably should be
  744. * using ilookup5() instead.
  745. *
  746. * Otherwise NULL is returned.
  747. *
  748. * Note, @test is called with the inode_lock held, so can't sleep.
  749. */
  750. struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
  751. int (*test)(struct inode *, void *), void *data)
  752. {
  753. struct hlist_head *head = inode_hashtable + hash(sb, hashval);
  754. return ifind(sb, head, test, data, 0);
  755. }
  756. EXPORT_SYMBOL(ilookup5_nowait);
  757. /**
  758. * ilookup5 - search for an inode in the inode cache
  759. * @sb: super block of file system to search
  760. * @hashval: hash value (usually inode number) to search for
  761. * @test: callback used for comparisons between inodes
  762. * @data: opaque data pointer to pass to @test
  763. *
  764. * ilookup5() uses ifind() to search for the inode specified by @hashval and
  765. * @data in the inode cache. This is a generalized version of ilookup() for
  766. * file systems where the inode number is not sufficient for unique
  767. * identification of an inode.
  768. *
  769. * If the inode is in the cache, the inode lock is waited upon and the inode is
  770. * returned with an incremented reference count.
  771. *
  772. * Otherwise NULL is returned.
  773. *
  774. * Note, @test is called with the inode_lock held, so can't sleep.
  775. */
  776. struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
  777. int (*test)(struct inode *, void *), void *data)
  778. {
  779. struct hlist_head *head = inode_hashtable + hash(sb, hashval);
  780. return ifind(sb, head, test, data, 1);
  781. }
  782. EXPORT_SYMBOL(ilookup5);
  783. /**
  784. * ilookup - search for an inode in the inode cache
  785. * @sb: super block of file system to search
  786. * @ino: inode number to search for
  787. *
  788. * ilookup() uses ifind_fast() to search for the inode @ino in the inode cache.
  789. * This is for file systems where the inode number is sufficient for unique
  790. * identification of an inode.
  791. *
  792. * If the inode is in the cache, the inode is returned with an incremented
  793. * reference count.
  794. *
  795. * Otherwise NULL is returned.
  796. */
  797. struct inode *ilookup(struct super_block *sb, unsigned long ino)
  798. {
  799. struct hlist_head *head = inode_hashtable + hash(sb, ino);
  800. return ifind_fast(sb, head, ino);
  801. }
  802. EXPORT_SYMBOL(ilookup);
  803. /**
  804. * iget5_locked - obtain an inode from a mounted file system
  805. * @sb: super block of file system
  806. * @hashval: hash value (usually inode number) to get
  807. * @test: callback used for comparisons between inodes
  808. * @set: callback used to initialize a new struct inode
  809. * @data: opaque data pointer to pass to @test and @set
  810. *
  811. * This is iget() without the read_inode() portion of get_new_inode().
  812. *
  813. * iget5_locked() uses ifind() to search for the inode specified by @hashval
  814. * and @data in the inode cache and if present it is returned with an increased
  815. * reference count. This is a generalized version of iget_locked() for file
  816. * systems where the inode number is not sufficient for unique identification
  817. * of an inode.
  818. *
  819. * If the inode is not in cache, get_new_inode() is called to allocate a new
  820. * inode and this is returned locked, hashed, and with the I_NEW flag set. The
  821. * file system gets to fill it in before unlocking it via unlock_new_inode().
  822. *
  823. * Note both @test and @set are called with the inode_lock held, so can't sleep.
  824. */
  825. struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
  826. int (*test)(struct inode *, void *),
  827. int (*set)(struct inode *, void *), void *data)
  828. {
  829. struct hlist_head *head = inode_hashtable + hash(sb, hashval);
  830. struct inode *inode;
  831. inode = ifind(sb, head, test, data, 1);
  832. if (inode)
  833. return inode;
  834. /*
  835. * get_new_inode() will do the right thing, re-trying the search
  836. * in case it had to block at any point.
  837. */
  838. return get_new_inode(sb, head, test, set, data);
  839. }
  840. EXPORT_SYMBOL(iget5_locked);
  841. /**
  842. * iget_locked - obtain an inode from a mounted file system
  843. * @sb: super block of file system
  844. * @ino: inode number to get
  845. *
  846. * This is iget() without the read_inode() portion of get_new_inode_fast().
  847. *
  848. * iget_locked() uses ifind_fast() to search for the inode specified by @ino in
  849. * the inode cache and if present it is returned with an increased reference
  850. * count. This is for file systems where the inode number is sufficient for
  851. * unique identification of an inode.
  852. *
  853. * If the inode is not in cache, get_new_inode_fast() is called to allocate a
  854. * new inode and this is returned locked, hashed, and with the I_NEW flag set.
  855. * The file system gets to fill it in before unlocking it via
  856. * unlock_new_inode().
  857. */
  858. struct inode *iget_locked(struct super_block *sb, unsigned long ino)
  859. {
  860. struct hlist_head *head = inode_hashtable + hash(sb, ino);
  861. struct inode *inode;
  862. inode = ifind_fast(sb, head, ino);
  863. if (inode)
  864. return inode;
  865. /*
  866. * get_new_inode_fast() will do the right thing, re-trying the search
  867. * in case it had to block at any point.
  868. */
  869. return get_new_inode_fast(sb, head, ino);
  870. }
  871. EXPORT_SYMBOL(iget_locked);
  872. /**
  873. * __insert_inode_hash - hash an inode
  874. * @inode: unhashed inode
  875. * @hashval: unsigned long value used to locate this object in the
  876. * inode_hashtable.
  877. *
  878. * Add an inode to the inode hash for this superblock.
  879. */
  880. void __insert_inode_hash(struct inode *inode, unsigned long hashval)
  881. {
  882. struct hlist_head *head = inode_hashtable + hash(inode->i_sb, hashval);
  883. spin_lock(&inode_lock);
  884. hlist_add_head(&inode->i_hash, head);
  885. spin_unlock(&inode_lock);
  886. }
  887. EXPORT_SYMBOL(__insert_inode_hash);
  888. /**
  889. * remove_inode_hash - remove an inode from the hash
  890. * @inode: inode to unhash
  891. *
  892. * Remove an inode from the superblock.
  893. */
  894. void remove_inode_hash(struct inode *inode)
  895. {
  896. spin_lock(&inode_lock);
  897. hlist_del_init(&inode->i_hash);
  898. spin_unlock(&inode_lock);
  899. }
  900. EXPORT_SYMBOL(remove_inode_hash);
  901. /*
  902. * Tell the filesystem that this inode is no longer of any interest and should
  903. * be completely destroyed.
  904. *
  905. * We leave the inode in the inode hash table until *after* the filesystem's
  906. * ->delete_inode completes. This ensures that an iget (such as nfsd might
  907. * instigate) will always find up-to-date information either in the hash or on
  908. * disk.
  909. *
  910. * I_FREEING is set so that no-one will take a new reference to the inode while
  911. * it is being deleted.
  912. */
  913. void generic_delete_inode(struct inode *inode)
  914. {
  915. struct super_operations *op = inode->i_sb->s_op;
  916. list_del_init(&inode->i_list);
  917. list_del_init(&inode->i_sb_list);
  918. inode->i_state|=I_FREEING;
  919. inodes_stat.nr_inodes--;
  920. spin_unlock(&inode_lock);
  921. security_inode_delete(inode);
  922. if (op->delete_inode) {
  923. void (*delete)(struct inode *) = op->delete_inode;
  924. if (!is_bad_inode(inode))
  925. DQUOT_INIT(inode);
  926. /* Filesystems implementing their own
  927. * s_op->delete_inode are required to call
  928. * truncate_inode_pages and clear_inode()
  929. * internally */
  930. delete(inode);
  931. } else {
  932. truncate_inode_pages(&inode->i_data, 0);
  933. clear_inode(inode);
  934. }
  935. spin_lock(&inode_lock);
  936. hlist_del_init(&inode->i_hash);
  937. spin_unlock(&inode_lock);
  938. wake_up_inode(inode);
  939. BUG_ON(inode->i_state != I_CLEAR);
  940. destroy_inode(inode);
  941. }
  942. EXPORT_SYMBOL(generic_delete_inode);
  943. static void generic_forget_inode(struct inode *inode)
  944. {
  945. struct super_block *sb = inode->i_sb;
  946. if (!hlist_unhashed(&inode->i_hash)) {
  947. if (!(inode->i_state & (I_DIRTY|I_LOCK)))
  948. list_move(&inode->i_list, &inode_unused);
  949. inodes_stat.nr_unused++;
  950. if (!sb || (sb->s_flags & MS_ACTIVE)) {
  951. spin_unlock(&inode_lock);
  952. return;
  953. }
  954. inode->i_state |= I_WILL_FREE;
  955. spin_unlock(&inode_lock);
  956. write_inode_now(inode, 1);
  957. spin_lock(&inode_lock);
  958. inode->i_state &= ~I_WILL_FREE;
  959. inodes_stat.nr_unused--;
  960. hlist_del_init(&inode->i_hash);
  961. }
  962. list_del_init(&inode->i_list);
  963. list_del_init(&inode->i_sb_list);
  964. inode->i_state |= I_FREEING;
  965. inodes_stat.nr_inodes--;
  966. spin_unlock(&inode_lock);
  967. if (inode->i_data.nrpages)
  968. truncate_inode_pages(&inode->i_data, 0);
  969. clear_inode(inode);
  970. wake_up_inode(inode);
  971. destroy_inode(inode);
  972. }
  973. /*
  974. * Normal UNIX filesystem behaviour: delete the
  975. * inode when the usage count drops to zero, and
  976. * i_nlink is zero.
  977. */
  978. void generic_drop_inode(struct inode *inode)
  979. {
  980. if (!inode->i_nlink)
  981. generic_delete_inode(inode);
  982. else
  983. generic_forget_inode(inode);
  984. }
  985. EXPORT_SYMBOL_GPL(generic_drop_inode);
  986. /*
  987. * Called when we're dropping the last reference
  988. * to an inode.
  989. *
  990. * Call the FS "drop()" function, defaulting to
  991. * the legacy UNIX filesystem behaviour..
  992. *
  993. * NOTE! NOTE! NOTE! We're called with the inode lock
  994. * held, and the drop function is supposed to release
  995. * the lock!
  996. */
  997. static inline void iput_final(struct inode *inode)
  998. {
  999. struct super_operations *op = inode->i_sb->s_op;
  1000. void (*drop)(struct inode *) = generic_drop_inode;
  1001. if (op && op->drop_inode)
  1002. drop = op->drop_inode;
  1003. drop(inode);
  1004. }
  1005. /**
  1006. * iput - put an inode
  1007. * @inode: inode to put
  1008. *
  1009. * Puts an inode, dropping its usage count. If the inode use count hits
  1010. * zero, the inode is then freed and may also be destroyed.
  1011. *
  1012. * Consequently, iput() can sleep.
  1013. */
  1014. void iput(struct inode *inode)
  1015. {
  1016. if (inode) {
  1017. struct super_operations *op = inode->i_sb->s_op;
  1018. BUG_ON(inode->i_state == I_CLEAR);
  1019. if (op && op->put_inode)
  1020. op->put_inode(inode);
  1021. if (atomic_dec_and_lock(&inode->i_count, &inode_lock))
  1022. iput_final(inode);
  1023. }
  1024. }
  1025. EXPORT_SYMBOL(iput);
  1026. /**
  1027. * bmap - find a block number in a file
  1028. * @inode: inode of file
  1029. * @block: block to find
  1030. *
  1031. * Returns the block number on the device holding the inode that
  1032. * is the disk block number for the block of the file requested.
  1033. * That is, asked for block 4 of inode 1 the function will return the
  1034. * disk block relative to the disk start that holds that block of the
  1035. * file.
  1036. */
  1037. sector_t bmap(struct inode * inode, sector_t block)
  1038. {
  1039. sector_t res = 0;
  1040. if (inode->i_mapping->a_ops->bmap)
  1041. res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
  1042. return res;
  1043. }
  1044. EXPORT_SYMBOL(bmap);
  1045. /**
  1046. * touch_atime - update the access time
  1047. * @mnt: mount the inode is accessed on
  1048. * @dentry: dentry accessed
  1049. *
  1050. * Update the accessed time on an inode and mark it for writeback.
  1051. * This function automatically handles read only file systems and media,
  1052. * as well as the "noatime" flag and inode specific "noatime" markers.
  1053. */
  1054. void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
  1055. {
  1056. struct inode *inode = dentry->d_inode;
  1057. struct timespec now;
  1058. if (IS_RDONLY(inode))
  1059. return;
  1060. if ((inode->i_flags & S_NOATIME) ||
  1061. (inode->i_sb->s_flags & MS_NOATIME) ||
  1062. ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)))
  1063. return;
  1064. /*
  1065. * We may have a NULL vfsmount when coming from NFSD
  1066. */
  1067. if (mnt &&
  1068. ((mnt->mnt_flags & MNT_NOATIME) ||
  1069. ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))))
  1070. return;
  1071. now = current_fs_time(inode->i_sb);
  1072. if (!timespec_equal(&inode->i_atime, &now)) {
  1073. inode->i_atime = now;
  1074. mark_inode_dirty_sync(inode);
  1075. }
  1076. }
  1077. EXPORT_SYMBOL(touch_atime);
  1078. /**
  1079. * file_update_time - update mtime and ctime time
  1080. * @file: file accessed
  1081. *
  1082. * Update the mtime and ctime members of an inode and mark the inode
  1083. * for writeback. Note that this function is meant exclusively for
  1084. * usage in the file write path of filesystems, and filesystems may
  1085. * choose to explicitly ignore update via this function with the
  1086. * S_NOCTIME inode flag, e.g. for network filesystem where these
  1087. * timestamps are handled by the server.
  1088. */
  1089. void file_update_time(struct file *file)
  1090. {
  1091. struct inode *inode = file->f_dentry->d_inode;
  1092. struct timespec now;
  1093. int sync_it = 0;
  1094. if (IS_NOCMTIME(inode))
  1095. return;
  1096. if (IS_RDONLY(inode))
  1097. return;
  1098. now = current_fs_time(inode->i_sb);
  1099. if (!timespec_equal(&inode->i_mtime, &now))
  1100. sync_it = 1;
  1101. inode->i_mtime = now;
  1102. if (!timespec_equal(&inode->i_ctime, &now))
  1103. sync_it = 1;
  1104. inode->i_ctime = now;
  1105. if (sync_it)
  1106. mark_inode_dirty_sync(inode);
  1107. }
  1108. EXPORT_SYMBOL(file_update_time);
  1109. int inode_needs_sync(struct inode *inode)
  1110. {
  1111. if (IS_SYNC(inode))
  1112. return 1;
  1113. if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
  1114. return 1;
  1115. return 0;
  1116. }
  1117. EXPORT_SYMBOL(inode_needs_sync);
  1118. /*
  1119. * Quota functions that want to walk the inode lists..
  1120. */
  1121. #ifdef CONFIG_QUOTA
  1122. /* Function back in dquot.c */
  1123. int remove_inode_dquot_ref(struct inode *, int, struct list_head *);
  1124. void remove_dquot_ref(struct super_block *sb, int type,
  1125. struct list_head *tofree_head)
  1126. {
  1127. struct inode *inode;
  1128. if (!sb->dq_op)
  1129. return; /* nothing to do */
  1130. spin_lock(&inode_lock); /* This lock is for inodes code */
  1131. /*
  1132. * We don't have to lock against quota code - test IS_QUOTAINIT is
  1133. * just for speedup...
  1134. */
  1135. list_for_each_entry(inode, &sb->s_inodes, i_sb_list)
  1136. if (!IS_NOQUOTA(inode))
  1137. remove_inode_dquot_ref(inode, type, tofree_head);
  1138. spin_unlock(&inode_lock);
  1139. }
  1140. #endif
  1141. int inode_wait(void *word)
  1142. {
  1143. schedule();
  1144. return 0;
  1145. }
  1146. /*
  1147. * If we try to find an inode in the inode hash while it is being
  1148. * deleted, we have to wait until the filesystem completes its
  1149. * deletion before reporting that it isn't found. This function waits
  1150. * until the deletion _might_ have completed. Callers are responsible
  1151. * to recheck inode state.
  1152. *
  1153. * It doesn't matter if I_LOCK is not set initially, a call to
  1154. * wake_up_inode() after removing from the hash list will DTRT.
  1155. *
  1156. * This is called with inode_lock held.
  1157. */
  1158. static void __wait_on_freeing_inode(struct inode *inode)
  1159. {
  1160. wait_queue_head_t *wq;
  1161. DEFINE_WAIT_BIT(wait, &inode->i_state, __I_LOCK);
  1162. wq = bit_waitqueue(&inode->i_state, __I_LOCK);
  1163. prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
  1164. spin_unlock(&inode_lock);
  1165. schedule();
  1166. finish_wait(wq, &wait.wait);
  1167. spin_lock(&inode_lock);
  1168. }
  1169. void wake_up_inode(struct inode *inode)
  1170. {
  1171. /*
  1172. * Prevent speculative execution through spin_unlock(&inode_lock);
  1173. */
  1174. smp_mb();
  1175. wake_up_bit(&inode->i_state, __I_LOCK);
  1176. }
  1177. static __initdata unsigned long ihash_entries;
  1178. static int __init set_ihash_entries(char *str)
  1179. {
  1180. if (!str)
  1181. return 0;
  1182. ihash_entries = simple_strtoul(str, &str, 0);
  1183. return 1;
  1184. }
  1185. __setup("ihash_entries=", set_ihash_entries);
  1186. /*
  1187. * Initialize the waitqueues and inode hash table.
  1188. */
  1189. void __init inode_init_early(void)
  1190. {
  1191. int loop;
  1192. /* If hashes are distributed across NUMA nodes, defer
  1193. * hash allocation until vmalloc space is available.
  1194. */
  1195. if (hashdist)
  1196. return;
  1197. inode_hashtable =
  1198. alloc_large_system_hash("Inode-cache",
  1199. sizeof(struct hlist_head),
  1200. ihash_entries,
  1201. 14,
  1202. HASH_EARLY,
  1203. &i_hash_shift,
  1204. &i_hash_mask,
  1205. 0);
  1206. for (loop = 0; loop < (1 << i_hash_shift); loop++)
  1207. INIT_HLIST_HEAD(&inode_hashtable[loop]);
  1208. }
  1209. void __init inode_init(unsigned long mempages)
  1210. {
  1211. int loop;
  1212. /* inode slab cache */
  1213. inode_cachep = kmem_cache_create("inode_cache",
  1214. sizeof(struct inode),
  1215. 0,
  1216. (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
  1217. SLAB_MEM_SPREAD),
  1218. init_once,
  1219. NULL);
  1220. set_shrinker(DEFAULT_SEEKS, shrink_icache_memory);
  1221. /* Hash may have been set up in inode_init_early */
  1222. if (!hashdist)
  1223. return;
  1224. inode_hashtable =
  1225. alloc_large_system_hash("Inode-cache",
  1226. sizeof(struct hlist_head),
  1227. ihash_entries,
  1228. 14,
  1229. 0,
  1230. &i_hash_shift,
  1231. &i_hash_mask,
  1232. 0);
  1233. for (loop = 0; loop < (1 << i_hash_shift); loop++)
  1234. INIT_HLIST_HEAD(&inode_hashtable[loop]);
  1235. }
  1236. void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
  1237. {
  1238. inode->i_mode = mode;
  1239. if (S_ISCHR(mode)) {
  1240. inode->i_fop = &def_chr_fops;
  1241. inode->i_rdev = rdev;
  1242. } else if (S_ISBLK(mode)) {
  1243. inode->i_fop = &def_blk_fops;
  1244. inode->i_rdev = rdev;
  1245. } else if (S_ISFIFO(mode))
  1246. inode->i_fop = &def_fifo_fops;
  1247. else if (S_ISSOCK(mode))
  1248. inode->i_fop = &bad_sock_fops;
  1249. else
  1250. printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o)\n",
  1251. mode);
  1252. }
  1253. EXPORT_SYMBOL(init_special_inode);