inode.c 38 KB

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