inode.c 44 KB

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