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