inode.c 44 KB

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