inode.c 43 KB

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