inode.c 47 KB

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