inode.c 42 KB

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