inode.c 44 KB

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